楔子
我似乎得了写完文章会回去自己读一遍的病,
而在之前写的关于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");
}
}
解释篇
其中你可以发现,他们的class都和它们的文件名称有关系,
而且它们都属于MonoBehaviour。
下面分别是TalkToMe和LearningScript的内容:
public class TalkToMe : MonoBehaviour
public class LearningScript : MonoBehaviour
那在下面,我们就按照顺序来解析一下这两个文件的其他地方。
附属体 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");
}
}
其定义了一个 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 ();
}
}
}
首先在这里,创建了 talkToMeComponent 这个变量,并将其送到隔壁的 TalkToMe 。
在这里,这个 TalkToMe 和 MonoBehavior 、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 到底是怎么出现的。

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

被折叠的 条评论
为什么被折叠?



