Unity3d中两种语言的对比JavaScript vs C#第二节
2011-09-08 14:12
344 查看
接上一教程, 在上一教程中主要对比了javascript和c#两种语言的基础知识点的区别,在这一节中作者主要对二者访问和控制场景对象及对象组件属性方面的异同进行了阐述,推荐大家阅读!
art 2 of a post series that tries to explain the differences betweenJavaScript and C# when programming for the Unity3D game engine. It is recommended that you read part one before continuing. In this post, I will explain how to access other GameObjects and
Components. This is one of he most common tasks that a programmer has to perform when writing scripts for Unity3D game engine. So, let’s start by assuming that we want to retrieve a GameObject named ‘Pawn’ which is at the root of the scene and has a script
called ‘PawnMover’ attached to it.
Getting the GameObject using JavaScript is simple. All you have to do is to call the GameObject class Find() static method and pass the name of the other GameObject we want as a parameter:
private var pawnGO:GameObject; function Awake() { pawnGO = GameObject.Find("Pawn");
}
This is how you do it with JavaScript. With C#, it will be very similar:
using UnityEngine;
using System.Collections;
public class PawnGetter : MonoBehaviour
{
private GameObject pawnGO;
void Awake ()
{
pawnGO = GameObject.Find("Pawn");
}
}
Without considering the two programming languages different notations and reserved keywords, the code is exactly the same (line 4 in the first block is the same as line 8 at the second). This makes sense, because it doesn’t matter if the code is strongly
typed or weakly typed,GameObject.Find() method always returns a GameObject.
Now, let’s see how to get a Component inside a GameObject. Again, assuming the same ‘Pawn’GameObject with a ‘PawnMover’ component attached to it, here is how to get the ‘PawnMover’ Component from another GameObject, using JavaScript:
private var pawnGO:GameObject;
private var pmSC:PawnMover;
function Awake()
{
pawnGO = GameObject.Find("Pawn");
pmSC = pawnGO.GetComponent("PawnMover");
}
Basically, to get the ‘PawnMover’ Component, all we needed to do is to call the GetComponent() method from the pawnGO GameObject and pass the desired component’s name as a parameter. Instead of the name, we could also have passed the type of the Component
as the parameter but, for the above example, the name will do. This method returns a Component, and since JavaScript is weakly typed, we don’t have to cast the resulting Component to the PawnMover class. The same script in C# will be:
using UnityEngine;
using System.Collections;
public class PawnGetter : MonoBehaviour
{
private GameObject pawnGO;
private PawnMover pmSC;
void Awake()
{
pawnGO = GameObject.Find("Pawn");
//returns a CS0266 error
pmSC = pawnGO.GetComponent("PawnMover");//<=returns a CS0266 error
//this is the right way to do it when using C#
pmSC = pawnGO.GetComponent< PawnMover >();
}
}
With C#, it isn’t possible to just call the GetComponent() method and pass the component’s name as a parameter, since it causes a CS0266 error, meaning that C# can’t implicitly convert from one type another. That’s because C# is strongly typed, so we can’t
convert from Component to PawnMover without a cast. That’s why we need to make a generic method call passing the type, thus, enforcing that the GetComponent() method returns a ‘PawnMover’ object instead of a Component.
That was a long post, but I hope there was something useful there. The next part of this series will explain some of the differences between JavaScript and C# when programming a script that moves a GameObject, which you can read here: Part 3 – Moving a GameObject.
art 2 of a post series that tries to explain the differences betweenJavaScript and C# when programming for the Unity3D game engine. It is recommended that you read part one before continuing. In this post, I will explain how to access other GameObjects and
Components. This is one of he most common tasks that a programmer has to perform when writing scripts for Unity3D game engine. So, let’s start by assuming that we want to retrieve a GameObject named ‘Pawn’ which is at the root of the scene and has a script
called ‘PawnMover’ attached to it.
Getting the GameObject using JavaScript is simple. All you have to do is to call the GameObject class Find() static method and pass the name of the other GameObject we want as a parameter:
private var pawnGO:GameObject; function Awake() { pawnGO = GameObject.Find("Pawn");
}
This is how you do it with JavaScript. With C#, it will be very similar:
using UnityEngine;
using System.Collections;
public class PawnGetter : MonoBehaviour
{
private GameObject pawnGO;
void Awake ()
{
pawnGO = GameObject.Find("Pawn");
}
}
Without considering the two programming languages different notations and reserved keywords, the code is exactly the same (line 4 in the first block is the same as line 8 at the second). This makes sense, because it doesn’t matter if the code is strongly
typed or weakly typed,GameObject.Find() method always returns a GameObject.
Now, let’s see how to get a Component inside a GameObject. Again, assuming the same ‘Pawn’GameObject with a ‘PawnMover’ component attached to it, here is how to get the ‘PawnMover’ Component from another GameObject, using JavaScript:
private var pawnGO:GameObject;
private var pmSC:PawnMover;
function Awake()
{
pawnGO = GameObject.Find("Pawn");
pmSC = pawnGO.GetComponent("PawnMover");
}
Basically, to get the ‘PawnMover’ Component, all we needed to do is to call the GetComponent() method from the pawnGO GameObject and pass the desired component’s name as a parameter. Instead of the name, we could also have passed the type of the Component
as the parameter but, for the above example, the name will do. This method returns a Component, and since JavaScript is weakly typed, we don’t have to cast the resulting Component to the PawnMover class. The same script in C# will be:
using UnityEngine;
using System.Collections;
public class PawnGetter : MonoBehaviour
{
private GameObject pawnGO;
private PawnMover pmSC;
void Awake()
{
pawnGO = GameObject.Find("Pawn");
//returns a CS0266 error
pmSC = pawnGO.GetComponent("PawnMover");//<=returns a CS0266 error
//this is the right way to do it when using C#
pmSC = pawnGO.GetComponent< PawnMover >();
}
}
With C#, it isn’t possible to just call the GetComponent() method and pass the component’s name as a parameter, since it causes a CS0266 error, meaning that C# can’t implicitly convert from one type another. That’s because C# is strongly typed, so we can’t
convert from Component to PawnMover without a cast. That’s why we need to make a generic method call passing the type, thus, enforcing that the GetComponent() method returns a ‘PawnMover’ object instead of a Component.
That was a long post, but I hope there was something useful there. The next part of this series will explain some of the differences between JavaScript and C# when programming a script that moves a GameObject, which you can read here: Part 3 – Moving a GameObject.
相关文章推荐
- unity3d中两种语言的对比JavaScript vs C#第一节
- unity3d中两种语言的对比JavaScript vs C# 第三节
- unity3d中两种语言的对比JavaScript vs C# 第四节
- unity3d中两种语言的对比JavaScript vs C# 第五节
- Unity3D中JavaScript与C#对比
- Unity3D中JavaScript与C#对比
- Unity3D中JavaScript与C#对比
- Unity3D中JavaScript与C#对比
- Unity3D中JavaScript与C#对比
- Boo Vs C# 语言对比 6
- 复杂事件处理两种技术实现手段的对比,规则语言 VS 持续查询语言
- Unity3D中JavaScript与C#对比
- VB,C#等语言得到java,javascript,actionscript等的getTime()函数的功能一致的方法
- 通过流程图对比编译型语言,解释型语言以及C#,JAVA的编译流程
- atitit.js javascript 调用c# java php后台语言api html5交互的原理与总结p97
- VB,C#等语言得到java,javascript,actionscript等的getTime()函数的功能一致的方法
- Mysql读写分离的两种实现对比:Spring+JPA应用层实现 vs Amoeba中间件实现
- C#读取Excel数据两种方式性能对比
- JAVA与C++两种语言的对比
- unity3d C#语言基础(继承)