Unity X C#小记之引用其他C#文件

本文介绍了Unity中如何在C#脚本之间进行引用和交互,通过LearningScript和TalkToMe两个示例脚本来讲解。在LearningScript中,获取并调用TalkToMe组件的方法,实现信息传递。同时,文中提到了Start()和Update()函数的使用,以及Unity Console的日志调试功能。

楔子


我似乎得了写完文章会回去自己读一遍的病,

而在之前写的关于Array/List/Dictionary的文章里有foreach和if,

但是忘记解释这两者以及它们的兄弟姐妹了Σ(  ̄□ ̄||)

算了找个空余的时间再补充吧 = 弃坑 ();

嘛,先让我过一遍关于第六章的难点吧。

注意:个人是零基础学习C#和Unity,在下文可能会有不正确的地方,若有大神指正则是万幸。

材:Learning C# by Developing Games with Unity 3D Beginner's Guide by Terry Norton

Unity版本:5.6.1f1


小记


准备篇


首先,如果你想要引用另外一份C#文件,那你得要有至少两个C#文件,

教材的例子将两个C#文件放到了MainCamera中,这两者分别是:

LearningScript

TalkToMe

LearningScript文件看起来是这样的:


using UnityEngine;
using System.Collections.Generic;

public class LearningScript : MonoBehaviour 
{
	TalkToMe talkToMeComponent;

	void Start ()
	{
		talkToMeComponent = GetComponent<TalkToMe> ();

		Debug.Log ("Press the Return key.");
	}

	void Update ()
	{
		if (Input.GetKeyDown (KeyCode.Return)) 
		{
			Debug.Log ("This is the TalkToMe Component: " + talkToMeComponent);
			Debug.Log (talkToMeComponent.hereItIs);
			talkToMeComponent.MakeMeTalk ();
		}
	}
}

引用且变体于 Learning C# by Developing Games with Unity 3D Beginner's Guide, Page 98,Terry Norton.


而TalkToMe则是这样的


using UnityEngine;
using System.Collections;

public class TalkToMe : MonoBehaviour 
{
	public string hereItIs = "This is the TalkToMe variable";
	public void MakeMeTalk ()
	{
		Debug.Log ("This is the TalkToMe mathod");
	}
}

引用 Learning C# by Developing Games with Unity 3D Beginner's Guide, Page 97,Terry Norton.


解释篇


其中你可以发现,他们的class都和它们的文件名称有关系,

而且它们都属于MonoBehaviour。

下面分别是TalkToMe和LearningScript的内容:


public class TalkToMe : MonoBehaviour

引用 Learning C# by Developing Games with Unity 3D Beginner's Guide,Page 97,Terry Norton.

public class LearningScript : MonoBehaviour

引用 Learning C# by Developing Games with Unity 3D Beginner's Guide,Page 98,Terry Norton.


那在下面,我们就按照顺序来解析一下这两个文件的其他地方。


附属体 TalkToMe


using UnityEngine;
using System.Collections;

public class TalkToMe : MonoBehaviour 
{
	public string hereItIs = "This is the TalkToMe variable";

	public void MakeMeTalk ()
	{
		Debug.Log ("This is the TalkToMe mathod");
	}
}

引用 Learning C# by Developing Games with Unity 3D Beginner's Guide,Page 97,Terry Norton.


其定义了一个 public string (公开的字符串),名称为 HereItIs 。

而这个 string 的内容就是双引号里面的 This is the TalkToMe variable 。

并行地,下方公开定义了一个函数,叫做 hereItIs () ,

这个 hereItIs () 函数里面其实就是在Unity的Console栏(用于 Debug )里面弹出一个 Log:

This is the TalkToMe method


主体LearningScript


using UnityEngine;
using System.Collections.Generic;

public class LearningScript : MonoBehaviour 
{
	TalkToMe talkToMeComponent;

	void Start ()
	{
		talkToMeComponent = GetComponent<TalkToMe> ();

		Debug.Log ("Press the Return key.");
	}

	void Update ()
	{
		if (Input.GetKeyDown (KeyCode.Return)) 
		{
			Debug.Log ("This is the TalkToMe Component: " + talkToMeComponent);

			Debug.Log (talkToMeComponent.hereItIs);
			talkToMeComponent.MakeMeTalk ();
		}
	}
}

引用且变体于 Learning C# by Developing Games with Unity 3D Beginner's Guide,Page 98,Terry Norton.


首先在这里,创建了 talkToMeComponent 这个变量,并将其送到隔壁的 TalkToMe

在这里,这个 TalkToMeMonoBehavior LearningScript的性质是一样的,是个 class (类)。

(注1:原本教材上写将这个别名码成 otherComponent ,但想到以后要养成习惯所以就不用这种写法。下同。)

(注2:其实我不喜欢把 class 翻译成“类”,可惜官方的翻译就是这个样子的...)

Component 有元器件的意思,而在Unity中这些文件都是元件,因此得名。

之后在下面分别定义了 Start () Update () 两个函数。

Start () 中,除了使用 GetComponent() 函数,将 TalkToMe 这个component 所依附的物体捕获且送到 talkToMeComponent

就是在Console栏 Log Press the Return key

Update () 函数则是 Log 出:

This is the TalkToMe Component :  (后面加上 talkToMeComponent 所指之物)

(输出 talkToMeComponent.HereItIs 这个 string ,这个 string 在talkToMeComponent 所指物的 HereItIs 上,也就是隔壁C#的 HereItIs

之后,再使用 talkToMeComponent.MakeMeTalk () 函数,也就是隔壁的 MakeMeTalk ()函数。


测试篇


如果在Console栏输出了以下内容:

Press the Return key.
UnityEngine.Debug:Log(Object)

This is the TalkToMe Component: Main Camera (TalkToMe)
UnityEngine.Debug:Log(Object)

This is the TalkToMe variable
UnityEngine.Debug:Log(Object)

This is the TalkToMe mathod
UnityEngine.Debug:Log(Object)

就算是成功的了。

当然,你可以点击 Log 里面的内容,它会在下面给出更多的相关信息。例如我左键点击:

This is the TalkToMe mathod
UnityEngine.Debug:Log(Object)

这条 Log 时,下面则会出现:

This is the TalkToMe mathod
UnityEngine.Debug:Log(Object)
TalkToMe:MakeMeTalk() (at Assets/Code/Script/TalkToMe.cs:11)
LearningScript:Update() (at Assets/Code/Script/LearningScript.cs:23)

它会说出这条 Log 到底是怎么出现的。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值