#私藏项目实操分享#【愚公系列】2022年02月 U3D全栈班 014-GameObject相关函数
  pjqvC5BRM2Nx 2023年11月02日 38 0

一、GameObject相关函数

函数

说明

GameObject()

构造函数,创建GameObject

SetActive()

Activates/Deactivates 启用/停用 GameObject

Destory()

销毁GameObject启用/停用GameObject

Instantiate()

克隆函数,克隆一个Object到当前场景中

GetComponent<T>()

通过GameObject实例获取组件

AddComponent<T>()

给GameObject实例添加组件

SendMessage()

调用GameObject的methodName方法

SendMessageUpwards()

调用GameObject以及所有父物体的methodName方法

BroadcastMessage()

调用GameObject以及所有子物体身上的methodName方法

注意:SendMessage方法,必须是GameObject身上挂载的脚本有methodName方法
具体使用方式:
1.定义两个对象

#私藏项目实操分享#【愚公系列】2022年02月 U3D全栈班 014-GameObject相关函数_构造函数

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Ryunm_ScriptsInUnity : MonoBehaviour
{
public GameObject qiaofeng;
public GameObject zhangwuji;

// Start is called before the first frame update
void Start()
{
//创建
GameObject newObj = new GameObject("xxx");
//启用
newObj.SetActive(true);
//克隆
GameObject cloneObj = Instantiate(newObj);
//添加组件
cloneObj.AddComponent<SpriteRenderer>();
//获取组件
var sr = cloneObj.GetComponent<SpriteRenderer>();
sr.sprite = firstSprite;
//销毁组件
Destroy(newObj,2);

}

// Update is called once per frame
//每帧都会执行一次
void Update()
{
//SendMessage执行游戏对象上的方法
if (Input.GetKeyDown(KeyCode.Q))
{
qiaofeng.SendMessage("GongFu_02");
}
if (Input.GetKeyDown(KeyCode.W))
{
qiaofeng.SendMessage("GongFu_01");
}
if (Input.GetKeyDown(KeyCode.E))
{
zhangwuji.SendMessage("GongFu_03");
}
if (Input.GetKeyDown(KeyCode.R))
{
zhangwuji.SendMessage("GongFu_04");
}

}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Ryunm_SendMessage : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{

}

public void GongFu_01()
{
print("独孤九剑");
}
public void GongFu_02()
{
print("降龙十八掌");
}
public void GongFu_03()
{
print("九阳真经");
}
public void GongFu_04()
{
print("六脉神剑");
}
}
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论