一、与物体相关的脚本
1.使用Resources加载Resources中的资源并克隆到场景中
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class test02 : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
//加载Resource文件夹中的英雄资源
GameObject hero = Resources.Load<GameObject>("hero/StoneKing");
//克隆物体到场景中
GameObject.Instantiate(hero);
}
}
复制代码
2.查找物体
1.通过路径查找物体
//这里举例找到Sphere对象后,停用物体,这样加载场景后就看不到该对象了
GameObject sphere= GameObject.Find("Sphere");
sphere.SetActive(false);//激活或停用对象
复制代码
2.通过标签查找物体
GameObject shpere = GameObject.FindGameObjectWithTag("Player");//括号中填标签名
shpere.SetActive(false);
复制代码
3.对物体的参数进行改变
GameObject shpere = GameObject.FindGameObjectWithTag("Player");//括号中填标签名
//shpere.SetActive(false);
shpere.name = "sphere01";//修改物体的名字
shpere.transform.position = new Vector3(0,10,34);//改变世界坐标
shpere.transform.localPosition = new Vector3(0, 10, 34);//改变相对自身起始点的坐标
shpere.transform.eulerAngles = new Vector3(34,32,1);//改变物体的旋转角度
shpere.transform.localEulerAngles=new Vector3(34, 32, 1);
shpere.transform.localScale = new Vector3(10,10,10);//改变物体的规模大小
复制代码
4.通过脚本删除物体
5.操作组件的相关脚本
GameObject shpere = GameObject.FindGameObjectWithTag("Player");//括号中填标签名
shpere.AddComponent<AudioSource>();//添加一个audio source组件
GameObject text = GameObject.FindGameObjectWithTag("Text");
//text.GetComponent<Text>().text="欢迎进入游戏";//查找自身上的Text组件
//text.GetComponentInChildren<Text>().text = "先查找孩子中是否有该组件再查找自身";
//text.GetComponentInParent<Text>().text = "先查找父亲中是否有该组件再查找自身";
//查找多个同类型的组件
AudioSource[] audioArray = text.GetComponentsInChildren<AudioSource>();//查找自身挂载的所有音频源组件
Debug.Log(audioArray.Length);
//激活和关闭组件 通过组件中的enabled属性,值为false关闭,ture激活
audioArray[0].enabled = false;
//可以使用Destory(传入要移除的组件)来移除组件
Destroy(audioArray[0]);
复制代码
二、像素游戏案例
1.控制虚拟轴移动物体
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class script : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//获取水平虚拟轴
float x = Input.GetAxis("Horizontal");
//获取垂直虚拟轴
float z = Input.GetAxis("Vertical");
Debug.Log($"x:{x},y:{z}");
//创建一个向量,该向量为玩家按下按键时的向量
Vector3 dir = new Vector3(x,0,z);
//判断玩家是否按下了按键,如果按下按键x,z的值一定会发生变化,这时候向量dir肯定不为0
if (dir != Vector3.zero)
{
//将玩家转dir向量的方向
transform.rotation = Quaternion.LookRotation(dir);
//因为update中是一帧一帧得执行,不同设备在一秒内可能执行的帧数不一样,这里我们想
//保持每隔一秒移动2m,可以乘以 Time.delta.Time表示每帧经过的时间
transform.Translate(Vector3.forward * 2 * Time.deltaTime);
}
}
}
复制代码
2.控制摄像机跟随
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cameral : MonoBehaviour
{
//保存开始时,玩家和相机的向量
private Vector3 vector;
//保存玩家的transform组件
private Transform player;
// Start is called before the first frame update
void Start()
{
//通过标签获取玩家的transform组件
player = GameObject.FindGameObjectWithTag("Player").transform;
//获取玩家和相机之间的向量
vector = player.position - transform.position;
}
// Update is called once per frame
void Update()
{
//每次更新相机的位置
transform.position = player.position - vector;
}
}
作者:用户8922541182901
链接:juejin.cn/post/716319…
来源:稀土掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。