zoukankan      html  css  js  c++  java
  • 事件

    如何使用事件创建动态的“广播”系统。

    事件

    EventManager

    using UnityEngine;
    using System.Collections;
    
    public class EventManager : MonoBehaviour 
    {
        public delegate void ClickAction();
        public static event ClickAction OnClicked;
    
    
        void OnGUI()
        {
            if(GUI.Button(new Rect(Screen.width / 2 - 50, 5, 100, 30), "Click"))
            {
                if(OnClicked != null)
                    OnClicked();
            }
        }
    }

    TeleportScript

    using UnityEngine;
    using System.Collections;
    
    public class TeleportScript : MonoBehaviour 
    {
        void OnEnable()
        {
            EventManager.OnClicked += Teleport;
        }
    
    
        void OnDisable()
        {
            EventManager.OnClicked -= Teleport;
        }
    
    
        void Teleport()
        {
            Vector3 pos = transform.position;
            pos.y = Random.Range(1.0f, 3.0f);
            transform.position = pos;
        }
    }

    TurnColorScript

    using UnityEngine;
    using System.Collections;
    
    public class TurnColorScript : MonoBehaviour 
    {
        void OnEnable()
        {
            EventManager.OnClicked += TurnColor;
        }
    
    
        void OnDisable()
        {
            EventManager.OnClicked -= TurnColor;
        }
    
    
        void TurnColor()
        {
            Color col = new Color(Random.value, Random.value, Random.value);
            renderer.material.color = col;
        }
    }
  • 相关阅读:
    windows常用命令行总结
    express安装
    MySQL去除外键关联关系
    c#实体转化
    C#之Clone
    mysql 将null转代为0(转)
    Mysql显示行号
    mysql存储过程游标加计划任务事件调度器
    mysql临时表
    Git学习笔记
  • 原文地址:https://www.cnblogs.com/Mr-Prince/p/14144159.html
Copyright © 2011-2022 走看看