zoukankan      html  css  js  c++  java
  • Unity3D笔记 英保通三 脚本编写 、物体间通信

    一、脚本编写

    1.1、同一类型的方法JS和C#的书写方式却不一样主要还是语法,在工程中创建一个Cube 分别把JSTest.js和CSharp.cs 添加到Cube中

    JSTest.js

    #pragma strict
    
    
    private var i:int;
    
    private var f:float;
    function Start () {
    
    }
    
    function Update () {
    
    }
    
    function SetInt(_i:int)
    {
        i=_i;
    }
    function SetFloat(_f:float)
    {
        f=_f;
    }
    
    function GetInt():int
    {
        return i;
    }
    
    function GetFloat():float
    {
        return f;
    }

    CSharp.cs

    using UnityEngine;
    using System.Collections;
    
    public class CSharpTest : MonoBehaviour {
    
        // Use this for initialization
        void Start () {
        
        }
        
        // Update is called once per frame
        void Update () {
        
        }
    
        int i;
        float f;
    
        public void SetInt(int i)
        {
            this.i = i;
        }
        public void SetFloat(float f)
        {
            this.f = f;
        }
    
        public int GetInt()
        {
            return i;
        }
    
        public float GetFloat()
        {
            return f;
        }
    }

    1.2、JS,C#调用方法赋给Main Camera 

    MainJS.js

    #pragma strict
    
    
    var obj:GameObject;
    function Start () {
        obj=GameObject.Find("Cube");
        
        var js:JSTest=obj.GetComponent(JSTest);
        
        js.SetInt(1);
        js.SetFloat(1.1);
        
        print("JS GetInt  :"+js.GetInt());
        
        print("JS GetFloat:"+js.GetFloat());
    }
    
    function Update () {
    
    }

    MainCharp.cs

    using UnityEngine;
    using System.Collections;
    
    public class MainCSharp : MonoBehaviour {
    
        GameObject obj;
        // Use this for initialization
        void Start () {
            obj = GameObject.Find ("Cube");
            CSharpTest cs = obj.GetComponent<CSharpTest> ();
            cs.SetInt (11);
            cs.SetFloat (11.11f);
    
            Debug.Log ("C# GetInt  "+cs.GetInt());
    
            Debug.Log ("C# GetFloat  "+cs.GetFloat ());
        }
        
        // Update is called once per frame
        void Update () {
        
        }
    }

    1.3、看看结果

    结果有点意外:JS应该先执行,为什么出来的顺序是这个?

      "Standard Assets"、 "Pro Standard Assets" 和 "Plugins" 这三个目录里的脚本被最先编译,"Editor"目录里的稍后编译,其他的脚本最后编译。

    二、物体间通信

      2.1、

      GameObject.SendMessage:向自身的脚本中发送消息
      GameObject.BroadcastMessage:向自身及子物体的脚本中发送消息
      GameObject.SendMessageUpwards:向自身及父物体中发送消息(先发给自己在发送给其他物体)

    例如:子类Sphere对应脚本sendMessage.cs 父类Cube对应脚本receiveMessage.cs

    sendMessage.cs 

    using UnityEngine;
    using System.Collections;
    
    public class sendMessage : MonoBehaviour {
    
        // Use this for initialization
        void Start () {
            gameObject.SendMessageUpwards("SendMsgParent", "Hello 来自子类Sphere");
        }
        
        // Update is called once per frame
        void Update () {
        
        }
    
        void SendMsgParent(string msg)
        {
            Debug.Log("我是子类Sphere,子类发来的消息:" + msg);
        }
    }

    receiveMessage.cs

    using UnityEngine;
    using System.Collections;
    
    public class receiveMessage : MonoBehaviour
    {
    
        // Use this for initialization
        void Start()
        {
    
        }
    
        // Update is called once per frame
        void Update()
        {
    
        }
    
        void SendMsgParent(string msg)
        {
            Debug.Log("我是父类Cube,子类发来的消息:" + msg);
        }
    }

    最终结果

    发送消息:先发送给自己在发送给其他物体

    2.2、上面有个缺陷就是必须是继承关系 方可传递消息

      解决方案:可以用C# 委托和事件机制来改变,可以实现不同物体间的进行消息传递

          步骤:新建两个GameObject :EventCube、ListenCube 做事件和监听。对应分别的cs文件EventCube.cs、ListenCube.cs

    EventCube.cs

    using UnityEngine;
    using System.Collections;
    
    public class EventCube : MonoBehaviour {
    
    
        public delegate void EventHandle(GameObject e);
    
        public event EventHandle MouseOver;
    
        void OnMouseOver()
        {
            if (MouseOver != null)
            {
                MouseOver(gameObject);
            }
    
        }
        // Use this for initialization
        void Start () {
        
        }
        
        // Update is called once per frame
        void Update () {
        
        }
    }

    ListenCube.cs

    using UnityEngine;
    using System.Collections;
    
    public class ListenCube : MonoBehaviour
    {
    
        // Use this for initialization
        void Start()
        {
            EventCube ev = GameObject.Find("EventCube").GetComponent<EventCube>();//EventCube 事件物体
            ev.MouseOver += new EventCube.EventHandle(ev_MouseOver);//监听函数
            //ev.MouseOver += ev_MouseOver;
        }
    
        void ev_MouseOver(GameObject e)
        {
            this.transform.Rotate(20, 0, 0);
            Debug.Log("旋转 :" + e);
            //throw new System.NotImplementedException();
        }
    
        void Listening(GameObject e)
        {
           
        }
    
        // Update is called once per frame
        void Update()
        {
    
        }
    }

    效果:


    作者:PEPE
    出处:http://pepe.cnblogs.com/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    LeetCode 32. 最长有效括号(Longest Valid Parentheses)
    LeetCode 141. 环形链表(Linked List Cycle)
    LeetCode 160. 相交链表(Intersection of Two Linked Lists)
    LeetCode 112. 路径总和(Path Sum)
    LeetCode 124. 二叉树中的最大路径和(Binary Tree Maximum Path Sum)
    LightGBM新特性总结
    sql service 事务与锁
    C#泛型实例详解
    C# 中的委托和事件(详解)
    C# DateTime日期格式化
  • 原文地址:https://www.cnblogs.com/PEPE/p/3590053.html
Copyright © 2011-2022 走看看