赞
踩
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
-
- public class scene1 : MonoBehaviour
- {
- public scene1 exclusive_scene1;
- public GameObject play;
- public GameObject cameraMain;//将只能拖拽照相机组件
- public int hp = 100;
- // Start is called before the first frame update
- void Start()
- {
-
-
- //组件的获取、禁用、调用其他脚本的方法
- //1.拖拽赋值获取组件
- //1_1.代码:public scene1 Newscene1;
- //1_2.要满足2个条件才能拖拽
- //1_2_1.要是scene1这个脚本
- //1_2_2.scene1要挂在了组件上
-
- //2.访问自身的组件 - GetComponent
- Transform t = GetComponent<Transform>();
- //Debug.Log(t);
- //2_1.因为Transform组件是个内置组件所以我们可以直接用开头小写的transform(当前组件)就可以访问到了
- //2_2.因为一个游戏物体身上是可以挂载多个相同的组件的所以,就有一个GetComponents
- Collider[] colliders = GetComponents<Collider>();
-
- foreach(Collider a in colliders) {
- //Debug.Log(a);
- };
-
- //2_3.如果你只想得到一个碰撞器(在组件上点击Add Component添加的末尾带collider的)的话
- //Debug.Log(GetComponent<BoxCollider>());
-
- //2_4.GetComponent不管是被借用了,它都是可以接着使用的
- //Debug.Log(GetComponent<scene1>());
-
- //2_5.获取子物体身上的组件就可以(GetComponentinChilidren)
- //待补充
-
- //2_6.获取另外一个物体身上的组件
- //Debug.Log(play.GetComponent<Rigidbody>());
-
- //3.禁用组件、改变组件属性
- //3_1.先要获取这个组件,比如要禁用这个钢体组件
- BoxCollider boxc = play.GetComponent<BoxCollider>();
- boxc.enabled = false; //禁用
-
- //3_2.将BoxCollider组件的勾去掉,即使组件被禁用了,这个里面的方法还是可以调用的
- boxc.enabled = false; //禁用
-
- //3_3.调用其他脚本的几种方法scene1 与 scene2_a
- //在后面
-
- //3_4.改变组件的属性
- Rigidbody rdy = play.GetComponent<Rigidbody>();
- rdy.mass = 300;
-
- //4.获取游戏物体的四种方式
- //4_1.直接拖拽(访问相机)public GameObject cameraMain;
- //4_2.通过Find查找,只能查找子物体,性能高
- //Debug.Log(transform.Find("GameObject1/GameObject11"));
- //4_3.(不推荐使用,耗性能,是全局查找)利用 GameObject.Find("游戏物体名");
- //Debug.Log(GameObject.Find("Main Camera"));
- //4_4.(推荐)通过标签查找untagged(未指定标签),点击Ad tag添加标签,也可以使用内置标签
- GameObject tag = GameObject.FindWithTag("play");
- //Debug.Log(tag);
- }
-
- // Update is called once per frame
- void Update()
- {
-
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
调用其他的脚本
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class scene2 : MonoBehaviour
- {
- // Start is called before the first frame update
- //5.调用其他脚本的几种方法
- //5_1.用new定义
- //scene1 t = new scene1();
- //暂不做探讨
-
- //5_2.用public定义类型
- public scene1 t;
-
- //5_3.可以用static添加静态.这样就可以直接scene1.Start();
- //暂不做探讨
-
- void Start()
- {
- t.enabled = false;//就算禁用了脚本,它的方法还是会执行的
- Debug.Log(t.hp);//打印要写方法里面,获取不要写在方法里面,不然不生效
-
- }
-
- // Update is called once per frame
- void Update()
- {
-
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。