unity 如何脚本间交互

简介: 如何脚本间交互:方法1:通过在编辑器里面拖动,来持有这个对象去调用对应的函数,这个方法比较简单。在编辑器中新建2个脚本。我们写一个a脚本public class Ascript : MonoBehaviour {// Use this for initializationvoid St...

如何脚本间交互:
方法1:

通过在编辑器里面拖动,来持有这个对象去调用对应的函数,这个方法比较简单。

在编辑器中新建2个脚本。

我们写一个a脚本

public class Ascript : MonoBehaviour {

// Use this for initialization

void Start () {

}

// Update is called once per frame

void Update () {

}

public void DoSomething()

{

Debug.LogWarning ("Ascript .. DoSomething .. Call");

}

}

我们想Main脚本去调用A脚本。我们就在Main 写一个A对象。

public Ascript ascript_;

这个是在代码编写的时候已经可以调用A对象的public 函数。

if (ascript_)

ascript_.DoSomething ();

但其实这个ascript_ 对象是空的,我们要对它赋值。这个时候我们在编辑器拖一个有a脚本的实体对象上去就可以了。

我们看见Debug.LogWarning ("Ascript .. DoSomething .. Call");有执行。

方法2:

我们把上面的直接调用改成

if (ascript_)

ascript_.SendMessage ("DoSomething");

我们把a脚本里面的DoSomething函数的public 去掉。

我们看见Debug.LogWarning ("Ascript .. DoSomething .. Call");有执行。

我们尝试给函数加一些参数,看看结果。

我们对A脚本的 DoSomething 进行修改。

void DoSomething(int param1)

{

Debug.LogWarning (string.Format("{0} {1}","Ascript .. DoSomething .. Call",param1));

}

如果我们脚本中有2个同名的函数会怎么样呢。

测试结果是谁在上面谁就会被调用。

可以在后面加一个参数。

if (ascript_) ascript_.SendMessage("DoSomething",2,SendMessageOptions.RequireReceiver);

我们把 a脚本的 DoSomething 函数删掉。

结果报错了......

我们把参数换了试试。

if (ascript_) ascript_.SendMessage("DoSomething",2,SendMessageOptions.DontRequireReceiver);

我们发现不报错了。

Public static
我们a脚本的DoSomething函数改成

public static void DoSomething()

{

Debug.LogWarning(string.Format("{0} {1}","Ascript .. DoSomething .. Call","static function"));

}

我们直接 Ascript.DoSomething ();调用就可以了。

我们再在这个基础上面改成下面这个样子。

public class Ascript : MonoBehaviour {

public static Ascript aStatic;

// Use this for initialization

void Start () {

aStatic = this;

}

// Update is called once per frame

void Update () {

}

public void DoSomething()

{

Debug.LogWarning(string.Format("{0} {1}","Ascript .. DoSomething .. Call","static Object"));

}

}

我们把调用的改成,Ascript.aStatic.DoSomething ();

我们这个时候已经不持有这个对象了。

但是场景一定要这个对象,如果没有的话会报错。

如果我们场景里面有很多这个对象,他会找到这个对象最早生成的那个,注意不是你创建的实体对象而是第一个拖上这个脚本的对象。

如何脚本间交互:
方法1:

通过在编辑器里面拖动,来持有这个对象去调用对应的函数,这个方法比较简单。

在编辑器中新建2个脚本。

我们写一个a脚本

public class Ascript : MonoBehaviour {

// Use this for initialization

void Start () {

}

// Update is called once per frame

void Update () {

}

public void DoSomething()

{

Debug.LogWarning ("Ascript .. DoSomething .. Call");

}

}

我们想Main脚本去调用A脚本。我们就在Main 写一个A对象。

public Ascript ascript_;

这个是在代码编写的时候已经可以调用A对象的public 函数。

if (ascript_)

ascript_.DoSomething ();

但其实这个ascript_ 对象是空的,我们要对它赋值。这个时候我们在编辑器拖一个有a脚本的实体对象上去就可以了。

我们看见Debug.LogWarning ("Ascript .. DoSomething .. Call");有执行。

方法2:

我们把上面的直接调用改成

if (ascript_)

ascript_.SendMessage ("DoSomething");

我们把a脚本里面的DoSomething函数的public 去掉。

我们看见Debug.LogWarning ("Ascript .. DoSomething .. Call");有执行。

我们尝试给函数加一些参数,看看结果。

我们对A脚本的 DoSomething 进行修改。

void DoSomething(int param1)

{

Debug.LogWarning (string.Format("{0} {1}","Ascript .. DoSomething .. Call",param1));

}

如果我们脚本中有2个同名的函数会怎么样呢。

测试结果是谁在上面谁就会被调用。

可以在后面加一个参数。

if (ascript_) ascript_.SendMessage("DoSomething",2,SendMessageOptions.RequireReceiver);

我们把 a脚本的 DoSomething 函数删掉。

结果报错了

我们把参数换了试试。

if (ascript_) ascript_.SendMessage("DoSomething",2,SendMessageOptions.DontRequireReceiver);

我们发现不报错了。

Public static
我们a脚本的DoSomething函数改成

public static void DoSomething()

{

Debug.LogWarning(string.Format("{0} {1}","Ascript .. DoSomething .. Call","static function"));

}

我们直接 Ascript.DoSomething ();调用就可以了。

我们再在这个基础上面改成下面这个样子。

public class Ascript : MonoBehaviour {

public static Ascript aStatic;

// Use this for initialization

void Start () {

aStatic = this;

}

// Update is called once per frame

void Update () {

}

public void DoSomething()

{

Debug.LogWarning(string.Format("{0} {1}","Ascript .. DoSomething .. Call","static Object"));

}

}

我们把调用的改成,Ascript.aStatic.DoSomething ();

我们这个时候已经不持有这个对象了。

但是场景一定要这个对象,如果没有的话会报下面的错。

如果我们场景里面有很多这个对象,他会找到这个对象最早生成的那个,注意不是你创建的实体对象而是第一个拖上这个脚本的对象。

单例模式

public class MyClass : MonoBehaviour {

void Awake () {

Debug.Log(Manager.Instance.myGlobalVar);

}}

public class Manager : Singleton {

protected Manager () {} // guarantee this will be always a singleton only - can't use the constructor!

public string myGlobalVar = "whatever";}

using UnityEngine;

///
/// Be aware this will not prevent a non singleton constructor
/// such as T myT = new T();
/// To prevent that, add protected T () {} to your singleton class.
///
/// As a note, this is made as MonoBehaviour because we need Coroutines.
///
public class Singleton : MonoBehaviour where T : MonoBehaviour
{
private static T _instance;

private static object _lock = new object();

public static T Instance
{
get
{
if (applicationIsQuitting) {
Debug.LogWarning("[Singleton] Instance '"+ typeof(T) +
"' already destroyed on application quit." +
" Won't create again - returning null.");
return null;
}

lock(_lock)
{
if (_instance == null)
{
_instance = (T) FindObjectOfType(typeof(T));

if ( FindObjectsOfType(typeof(T)).Length > 1 )
{
Debug.LogError("[Singleton] Something went really wrong " +
" - there should never be more than 1 singleton!" +
" Reopening the scene might fix it.");
return _instance;
}

if (_instance == null)
{
GameObject singleton = new GameObject();
_instance = singleton.AddComponent();
singleton.name = "(singleton) "+ typeof(T).ToString();

DontDestroyOnLoad(singleton);

Debug.Log("[Singleton] An instance of " + typeof(T) +
" is needed in the scene, so '" + singleton +
"' was created with DontDestroyOnLoad.");
} else {
Debug.Log("[Singleton] Using instance already created: " +
_instance.gameObject.name);
}
}

return _instance;
}
}
}

private static bool applicationIsQuitting = false;
///
/// When Unity quits, it destroys objects in a random order.
/// In principle, a Singleton is only destroyed when application quits.
/// If any script calls Instance after it have been destroyed,
/// it will create a buggy ghost object that will stay on the Editor scene
/// even after stopping playing the Application. Really bad!
/// So, this was made to be sure we're not creating that buggy ghost object.
///
public void OnDestroy () {
applicationIsQuitting = true;
}
}

更多unity2018的功能介绍请到paws3d爪爪学院查找。

相关文章
|
2月前
|
存储 人工智能 Java
Unity优化——脚本优化策略4
Unity优化——脚本优化策略4
|
10天前
|
存储 图形学
【unity小技巧】unity事件系统创建通用的对象交互的功能
【unity小技巧】unity事件系统创建通用的对象交互的功能
10 0
|
10天前
|
人工智能 图形学
【unity小技巧】使用动画状态机脚本实现一个简单3d敌人AI功能
【unity小技巧】使用动画状态机脚本实现一个简单3d敌人AI功能
9 0
|
10天前
|
人工智能 定位技术 图形学
【Unity小技巧】一个脚本实现控制3D远程/近战敌人AI
【Unity小技巧】一个脚本实现控制3D远程/近战敌人AI
6 0
|
10天前
|
自然语言处理 图形学
【unity实战】一个通用的FPS枪支不同武器射击控制脚本
【unity实战】一个通用的FPS枪支不同武器射击控制脚本
14 0
|
18天前
|
程序员 图形学 Android开发
Unity脚本生命周期
Unity脚本生命周期
|
2月前
|
存储 人工智能 缓存
Unity优化——脚本优化策略3
Unity优化——脚本优化策略3
|
2月前
|
存储 缓存 Java
Unity优化——脚本优化策略2
Unity优化——脚本优化策略2
|
2月前
|
存储 XML 缓存
Unity优化——脚本优化策略1
Unity优化——脚本优化策略1
|
2月前
|
API C# 图形学
【Unity 3D】常见API的讲解以及在C#脚本中的执行(附源码)
【Unity 3D】常见API的讲解以及在C#脚本中的执行(附源码)
97 1