博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Unity3D笔记 英保通三 脚本编写 、物体间通信
阅读量:7126 次
发布时间:2019-06-28

本文共 3957 字,大约阅读时间需要 13 分钟。

一、脚本编写

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

JSTest.js

#pragma strictprivate 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 strictvar 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
(); 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 事件物体 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() { }}

效果:

 

 

 

 

 

 

 

 

转载地址:http://crhel.baihongyu.com/

你可能感兴趣的文章
react-native搭建用例(非CRNA)
查看>>
HTTP协议
查看>>
github简单使用
查看>>
Python提取网站数据笔记
查看>>
隐私政策
查看>>
一些个人认为值得推荐的IT编程技术社区、博客或文章收集与分享
查看>>
排序算法性能比较
查看>>
Java设计模式-策略模式
查看>>
java B2B2C 源码 多级分销springmvc mybatis多租户电子商城系统-注册中心Eureka
查看>>
学习笔记(4.6)
查看>>
java B2B2C Springcloud多租户电子商城系统-spring-cloud-eureka
查看>>
掌握设计规范,UI设计师不得不知的三件事!
查看>>
The SQL vs NoSQL Difference: MySQL vs MongoDB
查看>>
武汉区块链软件技术公司:区块链+工业4.0对制造业的影响
查看>>
一文说透WordPress的自定义文章类型
查看>>
小白读源码 | RxJava2 入门篇(一)
查看>>
互联网安全内容安全及防护
查看>>
Spring Boot监控与管理的实现
查看>>
人工智能识别植物准确率高达80% 植物学家轻松了
查看>>
对于非技术人员来说,闪电网络和BCH分别意味着什么?
查看>>