重要的类
Transfrom
Object
GameObject
Component
Time
Animation
一、Component 找组件
属性:
GameObject
tag
transfromg
函数:
GetComponment
GetComponmentInChildren
GetComponmentInParent
GetComponmenstInChildren
GetComponmentsInParent
二、Transfrom
对物体进行位置,大小,缩放的改变,找孩子
变量:
position:The position of the transfrom in world space.在世界空间坐标transform的位置。
localposition: The position of the transfrom relative to parent transfrom.相对于父级的变换的位置。
LossyScale:相对于模型的,物体的全局缩放(只读)。
LocalScale:相对于父级物体变换的缩放。
函数:
Translate:function Translate (translation : Vector3, relativeTo : Space = Space.Self) : void
移动transform在translation的方向和距离。
function Update() {
// Move the object forward along its z axis 1 unit/second.
//沿着z轴1单位/秒,向前移动物体
transform.Translate(Vector3.forward * Time.deltaTime);
// Move the object upward in world space 1 unit/second.
//在世界坐标沿着y轴1单位/秒,向上移动物体
transform.Translate(Vector3.up * Time.deltaTime, Space.World);
}
function Translate (x : float, y : float, z : float, relativeTo : Space = Space.Self) : void
function Update() {
// Move the object forward along its z axis 1 unit/second.
//沿着z轴每秒1单位向前移动物体
transform.Translate(0, 0, Time.deltaTime);
// Move the object upward in world space 1 unit/second.
//在世界坐标每秒1单位向上移动物体
transform.Translate(0, Time.deltaTime, 0, Space.World);
}
• function Translate (translation : Vector3, relativeTo : Transform) : void
function Update() {
// Move the object to the right relative to the camera 1 unit/second.
//相对于摄像机每秒1单位向右移动物体
transform.Translate(Vector3.right * Time.deltaTime, Camera.main.transform);
}
function Translate (x : float, y : float, z : float, relativeTo : Transform) : void
function Update() {
// Move the object to the right relative to the camera 1 unit/second.
//相对于摄像机每秒1单位向右移动物体
transform.Translate(Time.deltaTime, 0, 0, Camera.main.transform);
}
Roate:
RoateAround
LookAt:注视旋转
Find:通过名字找
function Update() {
aFinger = transform.Find("LeftShoulder/Arm/Hand/Finger");
aFinger.Rotate(Time.deltaTime*20, 0, 0);
}
GitChild:Git Child(int index)根据索引查找
DetachChildren :解除父子关系
transform.DetachChildren();
Destroy(gameObject);
SetParent:
三、GameObject
变量:
active 是否活动
//使游戏物体不活动.
gameObject.active = false;
isStatic 是否静态
函数:
AddComponent
gameObject.AddComponent ("FoobarScript");
GetComponent
curTransform = gameObject.GetComponent(Transform);
Find
//这将返回名为Hand 的游戏物体
hand = GameObject.Find("Hand");
FindWithTag
//在标签为"Respawn"的游戏物体的位置实例化一个respawnPrefab.
var respawnPrefab : GameObject;
var respawn = GameObject.FindWithTag ("Respawn");
Instantiate (respawnPrefab, respawn.transform.position, respawn.transform.rotation);
FindGameObjectsWithTag
var respawns = GameObject.FindGameObjectsWithTag ("Respawn");
SetActive
四、Object
变量:
函数:
FindObjectOfType查找首个Type物体
// Search for any object of Type GUITexture,
// if found print its name, else print a message that says that it was not found.
//搜索任意GUITexture类型的物体
//如果发现打印它的名字,否则打印一条消息说没有发现
function Start()
{
var s : GUITexture = FindObjectOfType(GUITexture);
if(s)
Debug.Log("GUITexture object found: " + s.name);
else
Debug.Log("No GUITexture object could be found");
}
FindObjectsOfType查找Type物体
// When clicking on the object, it will disable all springs on all hinges in the scene.
//当点击物体,它将禁用场景中所有铰链中的弹簧
function OnMouseDown () {
var hinges : HingeJoint[] = FindObjectsOfType(HingeJoint) as HingeJoint[];
for (var hinge : HingeJoint in hinges) {
hinge.useSpring = false;
}
}
Destroy 销毁
// Kills the game object
//销毁游戏物体
Destroy (gameObject);
// Removes this script instance from the game object
//从游戏物体删除该脚本实例
Destroy (this);
// Removes the rigidbody from the game object
//从游戏物体删除刚体
Destroy (rigidbody);
// Kills the game object in 5 seconds after loading the object
//加载物体5秒后销毁游戏物体
Destroy (gameObject, 5);
Instantiate 实例
var clone : Missile;
clone = Instantiate(projectile, transform.position, transform.rotation);
更多unity2018的功能介绍请到paws3d爪爪学院查找。