[unity] 游戏内的红点系统

在游戏中,通常需要给玩家提示一些未完成的任务或者活动,这些提示以红点的形式展示,被称为红点系统。红点系统可以帮助玩家了解当前有哪些任务或者活动需要完成,从而提高玩家的参与度和粘性。

简单的

在游戏中实现红点系统的基本流程如下:

  • 确定需要显示红点的界面和位置:根据游戏需求,确定需要显示红点的界面和位置。

    确定红点显示条件:根据游戏逻辑,确定显示红点的条件。例如,在游戏中有一个任务列表,当列表中有未完成的任务时,需要在任务列表的图标上显示红点。

  • 监听数据变化:在游戏中,数据的变化会导致需要显示红点的条件也发生变化。因此,需要在游戏中监听数据变化,当数据发生变化时,重新判断是否需要显示红点。

  • 显示或隐藏红点:当需要显示红点时,在指定的界面和位置上显示红点;当不需要显示红点时,隐藏红点。

下面是一个简单的Unity C#代码示例,用于实现一个任务列表的红点系统:


public class TaskList : MonoBehaviour
{
    
    
    public GameObject redPoint; // 红点的GameObject

    private bool hasUnfinishedTask = false; // 是否有未完成的任务

    void Start()
    {
    
    
        // 监听任务状态的变化
        TaskManager.instance.onTaskStatusChanged += UpdateRedPoint;
    }

    void UpdateRedPoint()
    {
    
    
        // 判断是否需要显示红点
        if (TaskManager.instance.HasUnfinishedTask())
        {
    
    
            hasUnfinishedTask = true;
            redPoint.SetActive(true); // 显示红点
        }
        else
        {
    
    
            hasUnfinishedTask = false;
            redPoint.SetActive(false); // 隐藏红点
        }
    }
}

在这个示例中,我们定义了一个TaskList脚本来管理任务列表,并在Start函数中监听TaskManager中任务状态的变化。当任务状态发生变化时,UpdateRedPoint函数会被调用,根据当前是否有未完成的任务来决定是否需要显示红点。如果有未完成的任务,我们将hasUnfinishedTask设置为true,并将红点的GameObject设置为active;否则将hasUnfinishedTask设置为false,并将红点的GameObject设置为inactive。

复杂的

对于更复杂的游戏,红点系统可能需要支持多个条件的组合判断,以及对不同玩家状态的判断。

举个例子,假设我们有一个 RPG 游戏,每个玩家都有多个角色,每个角色都有自己的装备和技能。玩家的任务列表中可能有多个任务,每个任务可能都有多个子任务。红点系统需要根据不同的条件来判断每个任务或子任务是否需要显示红点。

在这种情况下,可以采用以下的方式来实现红点系统:

  • 创建一个 RedPointManager 类来管理所有红点逻辑。

  • 创建一个 RedPointNode 类来表示每个需要判断红点的对象。对于任务系统,可以创建一个 RedPointNode 对象来表示每个任务或子任务。

  • 在 RedPointNode 中定义需要判断的条件,例如:是否已完成、是否有奖励可以领取、是否有关联任务等。

  • 创建一个 RedPointCondition 类来表示单个条件,并在 RedPointNode 中维护多个 RedPointCondition 对象。

  • 在 RedPointManager 中维护所有 RedPointNode 对象,并在游戏中需要判断红点的地方调用 RedPointManager 的 Check 方法进行计算。

  • Check 方法遍历所有 RedPointNode,调用每个 RedPointNode 的 Check 方法,判断该节点是否需要显示红点。如果需要显示红点,则将该节点标记为红点状态。

  • 在游戏中需要显示红点的地方,读取 RedPointNode 的红点状态,并根据需要显示红点。

这样的实现方式可以支持多个条件的组合判断,以及对不同玩家状态的判断。在实际开发中,可以根据具体需求进行扩展和优化。

以下是一个简单的红点系统的示例代码,其中用到了观察者模式和递归算法:


public class RedDotManager : MonoBehaviour
{
    
    
    private static RedDotManager instance;

    public static RedDotManager Instance
    {
    
    
        get
        {
    
    
            if (instance == null)
            {
    
    
                instance = FindObjectOfType<RedDotManager>();
                if (instance == null)
                {
    
    
                    var go = new GameObject("RedDotManager");
                    instance = go.AddComponent<RedDotManager>();
                }
            }
            return instance;
        }
    }

    private Dictionary<string, bool> redDotDict = new Dictionary<string, bool>();

    public void AddRedDot(string key)
    {
    
    
        redDotDict[key] = true;
        NotifyObservers(key);
    }

    public void RemoveRedDot(string key)
    {
    
    
        redDotDict.Remove(key);
        NotifyObservers(key);
    }

    public bool HasRedDot(string key)
    {
    
    
        bool hasRedDot = false;
        if (redDotDict.TryGetValue(key, out hasRedDot))
        {
    
    
            return hasRedDot;
        }
        return false;
    }

    public void RegisterObserver(RedDotObserver observer)
    {
    
    
        observers.Add(observer);
    }

    public void UnregisterObserver(RedDotObserver observer)
    {
    
    
        observers.Remove(observer);
    }

    private List<RedDotObserver> observers = new List<RedDotObserver>();

    private void NotifyObservers(string key)
    {
    
    
        foreach (var observer in observers)
        {
    
    
            if (observer.WatchKey == key || IsAncestor(observer.WatchKey, key))
            {
    
    
                observer.UpdateRedDot();
            }
        }
    }

    private bool IsAncestor(string parent, string child)
    {
    
    
        if (child.StartsWith(parent + "/"))
        {
    
    
            return true;
        }
        else
        {
    
    
            int lastSlashIndex = child.LastIndexOf('/');
            if (lastSlashIndex >= 0)
            {
    
    
                string newChild = child.Substring(0, lastSlashIndex);
                return IsAncestor(parent, newChild);
            }
            else
            {
    
    
                return false;
            }
        }
    }
}

public interface RedDotObserver
{
    
    
    string WatchKey {
    
     get; }
    void UpdateRedDot();
}

public class RedDotButton : MonoBehaviour, RedDotObserver
{
    
    
    [SerializeField]
    private string watchKey;

    public string WatchKey
    {
    
    
        get {
    
     return watchKey; }
    }

    public void OnClick()
    {
    
    
        RedDotManager.Instance.RemoveRedDot(watchKey);
    }

    public void UpdateRedDot()
    {
    
    
        bool hasRedDot = RedDotManager.Instance.HasRedDot(watchKey);
        // Update UI with hasRedDot
    }
}

public class RedDotPanel : MonoBehaviour, RedDotObserver
{
    
    
    [SerializeField]
    private string watchKey;

    public string WatchKey
    {
    
    
        get {
    
     return watchKey; }
    }

    public void Start()
    {
    
    
        RedDotManager.Instance.RegisterObserver(this);
    }

    public void OnDestroy()
    {
    
    
        RedDotManager.Instance.UnregisterObserver(this);
    }

    public void UpdateRedDot()
    {
    
    
        bool hasRedDot = RedDotManager.Instance.HasRedDot(watchKey);
        // Update UI with hasRedDot
    }
}

public class RedDotAchievement : MonoBehaviour, RedDotObserver
{
    
    
    [SerializeField]
    private string watchKey;

    public string WatchKey
    {
    
    
        get {
    
     return watchKey; }
    }

    public void Start()
    {
    
    
        RedDotManager.Instance.RegisterObserver(this);
    }

    public void OnDestroy()
    {
    
    
           RedDotManager.Instance.UnregisterObserver(this);
     }
     
     public void UpdateRedDot()
    {
    
    
        bool hasRedDot = RedDotManager.Instance.HasRedDot(watchKey);
        // Update UI with hasRedDot
    }

猜你喜欢

转载自blog.csdn.net/hhh314159/article/details/129304665