一、脚本编写

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 initializationvoid Start () {}// Update is called once per framevoid 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 initializationvoid 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 framevoid 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 initializationvoid Start () {gameObject.SendMessageUpwards("SendMsgParent", "Hello 来自子类Sphere");}// Update is called once per framevoid Update () {}void SendMsgParent(string msg){Debug.Log("我是子类Sphere,子类发来的消息:" + msg);}
}

receiveMessage.cs

using UnityEngine;
using System.Collections;public class receiveMessage : MonoBehaviour
{// Use this for initializationvoid Start(){}// Update is called once per framevoid 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 initializationvoid Start () {}// Update is called once per framevoid Update () {}
}

ListenCube.cs

using UnityEngine;
using System.Collections;public class ListenCube : MonoBehaviour
{// Use this for initializationvoid 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 framevoid Update(){}
}

效果:

转载于:https://www.cnblogs.com/PEPE/p/3590053.html

Unity3D笔记 英保通三 脚本编写 、物体间通信相关推荐

  1. Unity3D笔记 英保通二

    一.访问另一个物体 1.代码中定义一个public的物体 例如:var target:Transform; 在面板上直接拖拽一个物体赋值给target 2.通过GameObject.Find(&quo ...

  2. Unity笔记 英保通 Unity新的动画系统Mecanim

    Mecanim动画系统是Unity独一无二.强大灵活的人物动画系统.该系统赋予您的人类和非人类人物令人难以置信的自然流畅的动作,使它们栩栩如生.游戏中角色设计提高到了新的层次,在处理人类动画角色中可以 ...

  3. 2021年大数据Kafka(三):❤️Kafka的集群搭建以及shell启动命令脚本编写❤️

    全网最详细的大数据Kafka文章系列,强烈建议收藏加关注! 新文章都已经列出历史文章目录,帮助大家回顾前面的知识重点. 目录 系列历史文章 Kafka的集群搭建以及shell启动命令脚本编写 一.搭建 ...

  4. ROS学习笔记三:编写第一个ROS节点程序

    在编写第一个ROS节点程序之前需要创建工作空间(workspace)和功能包(package). 一.创建工作空间(workspace) 创建一个catkin_ws: #注意:如果使用sudo一次性创 ...

  5. KETTLE调度第三篇:Windows下调度Dos脚本编写和遇到的一些问题解决

    KETTLE调度第三篇:Windows下调度Dos脚本编写和遇到的一些问题解决 参考文章: (1)KETTLE调度第三篇:Windows下调度Dos脚本编写和遇到的一些问题解决 (2)https:// ...

  6. Gatling学习笔记(四)---脚本编写及功能介绍

    文章目录 1.脚本编写 1.1 脚本示例 1.2 脚本编写 2.SSL使用 3.条件语句 4.Check和Session使用 5.Feeder 1.脚本编写 其实在压测的过程中我们主要也是压测http ...

  7. Android自动化测试之Monkey命令使用及monkey脚本编写

    系列文章 Android自动化测试环境部署及adb sdkmanager avdmanager Monitor DDMS工具使用及命令详解 Android自动化测试之Monkey使用及monkey脚本 ...

  8. Linux编写脚本查看mod,Linux shell脚本编写基础

    在进行linux测试时编写脚本是必不可少的,Shell脚本的名称可以随便定义,也不要什么后缀名,例如可以写abc,smartzip这类名称,运行时只要键入 ./smartzip就能运行脚本了.. 每行 ...

  9. 命令测试post_性能测试脚本编写之三

    >>>推荐阅读<<< 1.性能测试学习笔记-场景设计 2.性能测试的重要意义 3.性能分析流程及方法 4.应用系统性能调优之性能分析 ### web_url ### ...

  10. 【Linux入门基础知识】Linux 脚本编写基础

    1. Linux 脚本编写基础 1.1 语法基本介绍 1.1.1 开头 程序必须以下面的行开始(必须放在文件的第一行): #!/bin/sh 符号#!用来告诉系统它后面的参数是用来执行该文件的程序.在 ...

最新文章

  1. 腾讯离职,迪士尼给发了offer
  2. MySQL-Front,MySQL的企业管理器
  3. 修饰符(public/private/default/protected)
  4. oracle 填入编号,sql – 带填充模式的Oracle to_char格式编号(FM0000)
  5. memcache、Redis与MongoDB的学习-1
  6. win10 uwp 让焦点在点击在页面空白处时回到textbox中
  7. ASP人事工资管理系统毕设
  8. MyBatis基础入门--知识点总结
  9. 软件测试中80/20原则
  10. python 微博_用python发微博
  11. word文档通配符换行_Word文档每一行后面都有小箭头如何消除?
  12. 如何搜集你想要的信息
  13. [LeetCode javaScript] 881. 救生艇
  14. 关于H3C光模块和华为光模块的型号大全
  15. coures包下载和安装 可解决报错ImportError: No module named '_curses'
  16. 计算机多用户访问控制软件,Win10权限管理与多用户远程登录(多方案)
  17. sqli-labs第一关和第二关
  18. 生产和销售业务流程会计分录总结
  19. 基于MATLAB开发AUTOSAR软件应用层模块-part4.将MATLAB生成的ARXML文件导入到达芬奇Developer
  20. 【软件测试】现史上破坏性最强、最著名的五大软件Bug

热门文章

  1. java中堆和栈的区别_java中堆和栈的区别
  2. java反射创建字符串_Java反射
  3. 策略模式java8_Java设计模式:策略模式
  4. hot编码 字符one_使用字符级RNN进行名字分类
  5. 小米3c虚拟服务器,小米路由器3C固件逆向与测评-新手向
  6. 厄拉多塞筛法求素数 c语言,求质数(Prime Number 素数)的方法——厄拉多塞筛法...
  7. html php简单程序,PHP_php简单的分页程序,[code]html head me - phpStudy
  8. TensorFlow1.1搭建自编码网络
  9. 翻译: 4.1 多层感知器MLP Multilayer Perceptrons pytorch
  10. 极客大学架构师训练营 系统架构 分布式缓存 一致性哈希 Hash 第9课 听课总结