内容纲要

任务管理系统效果图

完成功能

  1. 显示任务面板、接受任务面板、完成任务面板
    2.接受任务、做任务、完成任务
    3.悬浮单个任务时可以显示任务的详细信息

任务类的信息与管理

public class TaskManage
{
    public static TaskManage instance = new TaskManage();

    public class Task
    {
        private int _id;//任务编号
        private int _typeId;//任务类型编号
        private string _title;//任务标题
        private string _content;//任务内容
        private string _award;//任务奖励提示
        private int _curValue;//任务当前完成次数
        private int _value;//任务需要完成的次数

        public Task()
        {

        }

        public Task(int id, int typeId, string title, string content, string award,int curValue,int value)
        {
            Id = id;
            TypeId = typeId;
            Title = title;
            Content = content;
            Award = award;
            CurValue = curValue;
            Value = value;
        }

        public int Id { get => _id; set => _id = value; }
        public int TypeId { get => _typeId; set => _typeId = value; }
        public string Title { get => _title; set => _title = value; }
        public string Content { get => _content; set => _content = value; }
        public string Award { get => _award; set => _award = value; }
        public int CurValue { get => _curValue; set => _curValue = value; }
        public int Value { get => _value; set => _value = value; }
    }

    //任务列表
    private Dictionary<int, Task> taskItems = new Dictionary<int, Task>();

    public void AddTask(Task task)
    {
        taskItems[task.Id] = task;
    }

    public Task GetItmes(int id)
    {
        return taskItems[id];
    }

    public int GetSize()
    {
        return taskItems.Count;
    }

    TaskManage()
    {
        AddTask(new Task(0, 0, "黄昏下的哀嚎", "强者的呼唤是敌是友只有前去查看才能得知", "获得未知怪物素材",0, 1));
        AddTask(new Task(1, 0, "天空王者", "在古代森林中获取到的重要信息,苍火龙的现身", "苍火龙的宝玉",0, 2));
        AddTask(new Task(2, 0, "那是?雷光虫!!", "森林的深处出现了不得了的事情,雷光虫的聚集,大地的震动都证明了会有不好的事情发生", "获得雷光虫素材",0, 5));
    }

}

该脚本用来显示接任务的面板和完成任务的面板

public class TaskViewManage : MonoBehaviour
{
    //创建单利
    public static TaskViewManage instance;
    Transform taskContent;
    Transform finishContent;

    //任务预制体
    public GameObject prefab;

    //任务面板
    public List<TaskManage.Task> TaskItems = new List<TaskManage.Task>();
    //完成任务面板
    private List<TaskManage.Task> FinishTaskItems = new List<TaskManage.Task>();

    private void Awake()
    {
        instance = this;

        //查找内容节点,将实例化的预制体任务对象添加到该内容节点的位置
        taskContent = transform.root.Find("TaskPanel/Task View/Viewport/TaskContent");
        finishContent = transform.root.Find("TaskPanel/Finish View/Viewport/FinishContent");

        //为任务面板添加任务
        for (int i = 0; i < TaskManage.instance.GetSize(); i++)
        {
            TaskItems.Add(TaskManage.instance.GetItmes(i));
        }

        //创建已经添加的任务
        for (int i = 0;i < TaskManage.instance.GetSize();i++)
        {
            //创建预制体
            GameObject item = Instantiate(prefab,Vector3.one,Quaternion.identity);
            //为预制体对象添加父母
            item.transform.SetParent(taskContent);
            //为预制体对象添加Toggle的分组
            item.GetComponent<Toggle>().group = taskContent.GetComponent<ToggleGroup>();
            //将获取到的信息添加给任务面板
            item.transform.Find("TaskTitle").GetComponent<Text>().text = TaskItems[i].Title;
            item.GetComponent<TaskItem>().id = TaskItems[i].Id;
            item.GetComponent<TaskItem>().typeId = TaskItems[i].TypeId;

        }
    }

    // Update is called once per frame
    void Update()
    {

    }

    public void AddButton()
    {
        //当我点击按钮时将会查找任务面板里面有哪些任务并且接到选中的那个任务
        for (int i = 0; i < taskContent.transform.childCount;i++) {
            //判断这个子对象是被选中的情况下
            if (taskContent.transform.GetChild(i).GetComponent<Toggle>().isOn)
            {
                int id = taskContent.transform.GetChild(i).GetComponent<TaskItem>().id;
                //将这个子对象放入接受任务的Panel里FinishPanel中,通过传递消息,将这个消息返回父亲在从父亲向下寻找AddTaskItem方法
                this.transform.parent.BroadcastMessage("AddTaskItem",id);
                break;
            }
        }
    }

    public void FinishTask(int id)
    {
        //创建出已完成的任务;
        GameObject go =Instantiate(prefab,finishContent);
        TaskManage.Task i = TaskManage.instance.GetItmes(id);
        TaskManage.Task item = new TaskManage.Task(i.Id, i.TypeId, i.Title, i.Content, i.Award, i.Value, i.Value);
        FinishTaskItems.Add(item);
        go.GetComponent<TaskItem>().id = item.Id;
        go.transform.Find("TaskTitle").GetComponent<Text>().text = item.Title;
        Destroy(go.GetComponent<Toggle>());
        go.transform.Find("BackImage").GetComponent<Image>().color = Color.yellow;
        go.GetComponent<Image>().color = Color.yellow;
    }

}

用来操作任务面板,将接受到的任务放入该面板中进行完成,完成的任务将会从任务面板中删除并且转移到完成任务面板中


public class FInishIngView : MonoBehaviour
{
    //获得预制体
    public GameObject taskPrefab;
    Transform content;//任务放入位置
    //已接受任务的集合
    private List<TaskManage.Task> taskItems = new List<TaskManage.Task>();

    private void Awake()
    {
        content = transform.Find("Scroll View/Viewport/Content");
    }

    // Update is called once per frame
    void Update()
    {

    }

    public void AddTaskItem(int id)
    {
        TaskManage.Task i = TaskManage.instance.GetItmes(id);
        TaskManage.Task item = new TaskManage.Task(i.Id,i.TypeId,i.Title,i.Content,i.Award,i.CurValue,i.Value);
        //添加任务项
        taskItems.Add(item);
        //实例化预制体,在任务面板里创建该任务预制体
        GameObject go = Instantiate(taskPrefab, content);
        //为预制体添加id,标题文本,Toggle分组

        go.GetComponent<TaskItem>().id = id;
        go.transform.Find("TaskTitle").GetComponent<Text>().text = item.Title;
        go.GetComponent<Toggle>().group = content.GetComponent<ToggleGroup>();
    }

    public void FinishButton()
    {
        for (int i = 0; i < content.transform.childCount; i++)
        {
            if (content.transform.GetChild(i).GetComponent<Toggle>().isOn)
            {
                taskItems[i].CurValue += 1;
                string titleText = string.Format("{0}({1}/{2})" ,taskItems[i].Title, taskItems[i].CurValue, taskItems[i].Value);
                content.transform.GetChild(i).Find("TaskTitle").GetComponent<Text>().text = titleText;
                if (taskItems[i].Value <= taskItems[i].CurValue)
                {
                    //将参数传入FinishTask方法后
                    this.transform.parent.BroadcastMessage("FinishTask", taskItems[i].Id);
                    //删除该list
                    taskItems.RemoveAt(i);
                    //删除该游戏对象
                    Destroy(content.transform.GetChild(i).gameObject);
                    //自动选择下一个孩子对象,如果没有下一个孩子对象将不执行
                    if ( i + 1 <= content.transform.childCount - 1)
                        content.transform.GetChild(i+1).GetComponent<Toggle>().isOn = true;
                }
                break;
            }
        }
    }

}

用来显示任务详细信息

public class Tooltip : MonoBehaviour
{
    public static Tooltip instance;

    public Text title,content, award;

    private void Awake()
    {
        instance = this;
        this.GetComponent<CanvasGroup>().alpha = 0;
    }

    // Update is called once per frame
    void Update()
    {
        this.transform.position  = Input.mousePosition;
    }

    public void ShowTooltip(int taskId)
    {
        this.GetComponent<CanvasGroup>().alpha = 1;
        TaskManage.Task taskItem = TaskManage.instance.GetItmes(taskId);
        if (taskItem == null)
        {
            Debug.Log(string.Format("要显示Tooltip的物品(id = {0})不存在", taskId));
        }
        title.text = taskItem.Title;
        content.text = taskItem.Content;
        award.text = taskItem.Award;
    }

    public void HidTooltip()
    {
        this.GetComponent<CanvasGroup>().alpha = 0;
    }
}

发表评论