位置: 编程技术 - 正文

Unity脚本C#参数相互传递(unity脚本调用)

编辑:rootadmin

推荐整理分享Unity脚本C#参数相互传递(unity脚本调用),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:unity脚本怎么用vs打开,unity常用的800个脚本,unity3d脚本语言c#,unity常用的800个脚本,unity脚本怎么用vs打开,unity c++脚本,unity3d脚本语言c#,unity c++脚本,内容如对您有帮助,希望把文章链接给更多的朋友!

转载自: 类名.参数 就可以调用。

发送消息

内容Component.SendMessage来实现跨脚本发送消息传递参数。下面发一个整理好的发消息脚本:

代码很简单。大家应该看一下就懂了。

using System;using System.Collections;using System.Collections.Generic;using UnityEngine; public class NotificationCenter : MonoBehaviour{ private static NotificationCenter mInstance; private Hashtable mHashtable; //单例 public static NotificationCenter GetInstance() { if (mInstance == null) { GameObject go = new GameObject("NotificationCenter"); mInstance = go.AddComponent<NotificationCenter>(); } return mInstance; } private NotificationCenter() { mHashtable = new Hashtable(); } public void AddSceneMessage(object observer, string name, object sender) { if (string.IsNullOrEmpty(name)) { Debug.LogError("传入名称错误"); return; } if (mHashtable[name] == null) { mHashtable[name] = new List<object>(); } List<object> listNotify = (List<object>)mHashtable[name]; if (listNotify.Contains(observer) == false) { listNotify.Add(observer); } } public void PostNotification(object aSender, string aName, object aData) { NotificationData ev = new NotificationData(aSender, aName, aData); if (string.IsNullOrEmpty(ev.name)) { return; } List<object> listNotify = (List<object>)mHashtable[ev.name]; if (listNotify != null) { List<object> listRemoveObserver = new List<object>(); for (int i = 0; i < listNotify.Count; i++) { object ob = listNotify[i]; MonoBehaviour observer = (MonoBehaviour)ob; if (!observer) { listRemoveObserver.Add(ob); } else { observer.SendMessage(ev.name, ev, SendMessageOptions.DontRequireReceiver); } } for (int i = 0; i < listRemoveObserver.Count; i++) { object ob = listRemoveObserver[i]; listNotify.Remove(ob); } } }}public class NotificationData{ public object sender; public string name; public object data; public NotificationData(object aSender, string aName, object aData) { sender = aSender; name = aName; data = aData; }}

用时。先注册下要发送到那个脚本,示例如下:

usingUnityEngine;usingSystem.Collections; publicclassN2:MonoBehaviour{ voidStart() { Debug.Log("n2 Start"); //注册消息 NotificationCenter.GetInstance().AddSceneMessage(this,"N2Test",null); //发送消息 NotificationCenter.GetInstance().PostNotification(this,"NTest","是N2调用的");} voidN2Test(NotificationDataev) { Debug.Log("调用了N2---"&#;(int)ev.data); }}

using UnityEngine;using System.Collections; public class N : MonoBehaviour { // Use this for initializationvoid Start () { NotificationCenter.GetInstance().AddSceneMessage(this, "NTest", null);} void NTest(NotificationData ev) { Debug.Log("调用了N---内容:"+(string)ev.data); NotificationCenter.GetInstance().PostNotification(this, "N2Test", ); }}

如上两个测试脚本。互相传递参数。有N2发消息到N。然后N在次发送到N2。 由于传递的是object。所以。可以发送任意类型的参数。

Unity脚本C#参数相互传递(unity脚本调用)

结果图:

委托

用delegate回调。如下。

usingUnityEngine;usingSystem.Collections; publicclassN3:MonoBehaviour{ voidStart() { Debug.Log("N3 start"); N.GetInstance().onPostData=N3Test; N.GetInstance().NTest();} voidN3Test(objectdata) { Debug.Log("N3 回调内容:"&#;(string)data); }}

usingUnityEngine;usingSystem.Collections; publicclassN:MonoBehaviour{ privatestaticNmInstance; //单例 publicstaticNGetInstance() { if(mInstance==null) { GameObjectgo=newGameObject("N"); mInstance=go.AddComponent<N>(); } returnmInstance; } publicdelegatevoidOnPostData(objectdata); publicOnPostDataonPostData; publicvoidNTest() { Debug.Log("N"); this.onPostData(""); }}

结果:

Unity3d 基于物理渲染Physically-Based Rendering之specular BRDF 在实时渲染中Physically-BasedRendering(PBR)中文为基于物理的渲染它能为渲染的物体带来更真实的效果,而且能量守恒稍微解释一下字母的意思,为对后文

Unity3D学习笔记之七创建自己的游戏场景 到现在为止我们已经拥有了比较完备的Prefab,已经可以创建宏大的游戏场景,并以第一人称视角在场景中漫游了。这里给大家做个小的示范,建一个小

Unity3d 基于物理渲染Physically-Based Rendering之实现 根据前文的例子

本文链接地址:https://www.jiuchutong.com/biancheng/383571.html 转载请保留说明!

上一篇:学习shader之前必须知道的东西之计算机图形学(一)渲染管线(shader能做什么)

下一篇:Unity3d 基于物理渲染Physically-Based Rendering之specular BRDF(unity3d基于物理系统的2D平台跳跃游戏)

免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

鄂ICP备2023003026号

网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

友情链接: 武汉网站建设 电脑维修 湖南楚通运网络