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/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    8款最新CSS3表单 环形表单很酷
    8款给力HTML5/CSS3应用插件 可爱的HTML5笑脸
    分享10款效果惊艳的HTML5图片特效
    9款极具创意的HTML5/CSS3进度条动画
    分享9款最新超酷HTML5/CSS3应用插件
    7款HTML5精美应用教程 让你立即爱上HTML5
    (转)整理 node-sass 安装失败的原因及解决办法
    分析Vue框架源码心得
    Vue中的render函数随笔
    微信小程序商业级实战
  • 原文地址:https://www.cnblogs.com/PEPE/p/3590053.html
Copyright © 2011-2022 走看看