unity脚本开发:一些重要的类和类间的关系图

本文深入解析Unity2018中的关键组件和功能,包括GameObject、Component、Transform及其应用,如位置、旋转和缩放的修改,以及如何使用Translate、Rotate和Find等函数。此外,还介绍了GameObject的激活状态、静态属性和组件添加方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

重要的类
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爪爪学院查找。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值