一、重要属性

1-1.获取自己依附的GameObject

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lesson3 : MonoBehaviour
{
private void Start()
{
//Mono里已经封装好了属性gameObject
//可以通过gameObject属性来获取
//(this.是可以省略的,为了便于理解 在前面加上this)
//打印出它的名字
print(this.gameObject.name);
}
}

1-2.获取自己依附的GameObject的位置信息

using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class Lesson3 : MonoBehaviour
{
private void Start()
{
//使用transform.出来
//(this.是可以省略的,为了便于理解 在前面加上this)
//获取坐标
print(this.transform.position);
//获取角度(欧拉角)
print(this.transform.eulerAngles);
//获取缩放
print(this.transform.lossyScale); //这种写法和上面是一样的
print(this.gameObject.transform);
}
}

1-3.设置脚本的 激活 与 失活

using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class Lesson3 : MonoBehaviour
{
private void Start()
{
//(this.是可以省略的,为了便于理解 在前面加上this)
//激活
this.enabled = true;
//失活
this.enabled = false;
}
}

1-4.获取别的游戏对象的gameObject和transform

using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class Lesson3 : MonoBehaviour
{
//因为两个物体挂的都是Lesson3脚本
//所以声明一个Lesson3类型的变量
public Lesson3 otherLesson3; private void Start()
{
//获取别的脚本对象依附的gameobject和transform信息
//(this.是可以省略的,为了便于理解 在前面加上this)
print(otherLesson3.gameObject.name);
print(otherLesson3.transform.position);
}
}



打印结果:

二、重要方法

主要内容:得到依附游戏对象上挂载的其它脚本

只要得到了场景中游戏物体 或 它身上依附的脚本,那么 就可以得到它的一切信息

2-1.得到和自己依附在同一游戏物体上的另一个脚本(很少用)

现有一个Lesson3物体,上面挂载了两个脚本



脚本Lesson3代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class Lesson3 : MonoBehaviour
{
private void Start()
{
//因为我们知道要获取的脚本是Lesson3_Test,所以直接使用Lesson3_Test类型的变量去接收它的返回值
Lesson3_Test t = null; //得到和自己依附在同一游戏物体上的另一个脚本
//1.根据脚本名获取(很少用)
// 需要用里氏替换原则把返回值 as 成Lesson3_Test
// 如果没找到""里的那个脚本,就默认返回null
t = this.GetComponent("Lesson3_Test") as Lesson3_Test;
print(t); //2.根据Type获取(反射的知识)
// 需要用里氏替换原则把返回值 as 成Lesson3_Test
t = this.GetComponent(typeof(Lesson3_Test)) as Lesson3_Test;
print(t); //3.通过泛型获取(用的最多,因为不用as了)
// 默认就返回Lesson3_Test类型,不需要as转换了
t = this.GetComponent<Lesson3_Test>();
print(t);
}
}

运行:

2-2.得到自己挂载的多个脚本

现有一个Lesson3物体,上面挂载了两个脚本

Lesson3脚本的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lesson3 : MonoBehaviour
{
private void Start()
{
//1.用数组得到Lesson3上挂载的所有脚本
//需要用一个Lesson3类型的数组去接收返回值
Lesson3[] array = this.GetComponents<Lesson3>();
print(array.Length); //2.用List得到Lesson3上挂载的所有脚本
//声明一个Lesson3类型的List
List<Lesson3> list = new List<Lesson3>();
//用这个List去存获取的结果
this.GetComponents<Lesson3>(list);
print(list.Count);
}
}

运行:

------------恢复内容开始------------

# 一、重要属性

1-1.获取自己依附的GameObject

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lesson3 : MonoBehaviour
{
private void Start()
{
//Mono里已经封装好了属性gameObject
//可以通过gameObject属性来获取
//(this.是可以省略的,为了便于理解 在前面加上this)
//打印出它的名字
print(this.gameObject.name);
}
}

1-2.获取自己依附的GameObject的位置信息

using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class Lesson3 : MonoBehaviour
{
private void Start()
{
//使用transform.出来
//(this.是可以省略的,为了便于理解 在前面加上this)
//获取坐标
print(this.transform.position);
//获取角度(欧拉角)
print(this.transform.eulerAngles);
//获取缩放
print(this.transform.lossyScale); //这种写法和上面是一样的
print(this.gameObject.transform);
}
}

1-3.设置脚本的 激活 与 失活

using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class Lesson3 : MonoBehaviour
{
private void Start()
{
//(this.是可以省略的,为了便于理解 在前面加上this)
//激活
this.enabled = true;
//失活
this.enabled = false;
}
}

1-4.获取别的游戏对象的gameObject和transform

using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class Lesson3 : MonoBehaviour
{
//因为两个物体挂的都是Lesson3脚本
//所以声明一个Lesson3类型的变量
public Lesson3 otherLesson3; private void Start()
{
//获取别的脚本对象依附的gameobject和transform信息
//(this.是可以省略的,为了便于理解 在前面加上this)
print(otherLesson3.gameObject.name);
print(otherLesson3.transform.position);
}
}





打印结果:

二、重要方法

主要内容:得到依附游戏对象上挂载的其它脚本

只要得到了场景中游戏物体 或 它身上依附的脚本,那么 就可以得到它的一切信息

2-1.得到和自己依附在同一游戏物体上的另一个脚本(很少用)

现有一个Lesson3物体,上面挂载了两个脚本



脚本Lesson3代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class Lesson3 : MonoBehaviour
{
private void Start()
{
//因为我们知道要获取的脚本是Lesson3_Test,所以直接使用Lesson3_Test类型的变量去接收它的返回值
Lesson3_Test t = null; //得到和自己依附在同一游戏物体上的另一个脚本
//1.根据脚本名获取(很少用)
// 需要用里氏替换原则把返回值 as 成Lesson3_Test
// 如果没找到""里的那个脚本,就默认返回null
t = this.GetComponent("Lesson3_Test") as Lesson3_Test;
print(t); //2.根据Type获取(反射的知识)
// 需要用里氏替换原则把返回值 as 成Lesson3_Test
t = this.GetComponent(typeof(Lesson3_Test)) as Lesson3_Test;
print(t); //3.通过泛型获取(用的最多,因为不用as了)
// 默认就返回Lesson3_Test类型,不需要as转换了
t = this.GetComponent<Lesson3_Test>();
print(t);
}
}

运行:

2-2.得到自己挂载的多个脚本

现有一个Lesson3物体,上面挂载了两个脚本

Lesson3脚本的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lesson3 : MonoBehaviour
{
private void Start()
{
//1.用数组得到Lesson3上挂载的所有脚本
//需要用一个Lesson3类型的数组去接收返回值
Lesson3[] array = this.GetComponents<Lesson3>();
print(array.Length); //2.用List得到Lesson3上挂载的所有脚本
//声明一个Lesson3类型的List
List<Lesson3> list = new List<Lesson3>();
//用这个List去存获取的结果
this.GetComponents<Lesson3>(list);
print(list.Count);
}
}

运行:

2-3.得到子对象挂载的脚本

(它默认也会找自己身上是否挂载了该脚本)

(儿子的儿子也会去找)

现有:



Lesson3脚本代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lesson3 : MonoBehaviour
{
private void Start()
{
//因为我们知道要获取的脚本是Lesson3_Test,所以直接使用Lesson3_Test类型的变量去接收它的返回值
Lesson3_Test t = null; //得到单个
//使用GetComponentInChildren<>来获取子对象挂载的某个脚本
//()中的参数:是否检测失活的,不填默认false
t = this.GetComponentInChildren<Lesson3_Test>(true);
print(t); //得到多个
//使用GetComponentsInChildren<>来获取子对象挂载的多个脚本
//方法一:用数组
//用一个Lesson3_Test类型的数组去接收返回值
Lesson3_Test[] array = this.GetComponentsInChildren<Lesson3_Test>(true);
print(array.Length); //方法二:用List得到Lesson3的子对象上挂载的所有脚本
//声明一个Lesson3类型的List
List<Lesson3_Test> list = new List<Lesson3_Test>();
//把获取到的子对象的脚本装入list
this.GetComponentsInChildren<Lesson3_Test>(true,list);
print(list.Count);
}
}

运行:

2-4.得到父对象挂载的脚本

(它默认也会找自己身上是否挂载了该脚本)

(父亲的父亲也会去找)

现有:



Lesson3脚本的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class Lesson3 : MonoBehaviour
{
private void Start()
{
//因为我们知道要获取的脚本是Lesson3_Test,所以直接使用Lesson3_Test类型的变量去接收它的返回值
Lesson3_Test t = null; //得到单个
//使用GetComponentInParent<>来获取父对象挂载的某个脚本
t = this.GetComponentInParent<Lesson3_Test>();
print(t); //得到多个
//使用GGetComponentsInParent<>来获取父对象挂载的多个脚本
//方法一:用数组
//用一个Lesson3_Test类型的数组去接收返回值
Lesson3_Test[] array = this.GetComponentsInParent<Lesson3_Test>();
print(array.Length); //方法二:用List存Lesson3的父对象上挂载的所有脚本
//声明一个Lesson3类型的List
List<Lesson3_Test> list = new List<Lesson3_Test>();
//把获取到的父对象的脚本装入list
this.GetComponentsInParent<Lesson3_Test>(true,list);
print(list.Count);
}
}

运行:

2-5.尝试获取脚本

之前在得单个脚本的时候,有可能没得到 为null

为了保险起见,往往得到脚本后会先判断它不为空 再进行逻辑处理

所以提供了一个更加安全的方法this.TryGetComponent<>

using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class Lesson3 : MonoBehaviour
{
private void Start()
{
//因为我们知道要获取的脚本是Lesson3_Test,所以直接使用Lesson3_Test类型的变量去接收它的返回值
Lesson3_Test t = null; //this.TryGetComponent<>会有一个bool型的返回值
//通过out把得到的脚本装进t
if (this.TryGetComponent<Lesson3_Test>(out t))
{
print("成功获取了");
}
}
}

最新文章

  1. 【LeetCode】Palindrome Pairs(336)
  2. .net 之缓存
  3. 。U盘安装CentOS6.5
  4. awk中可以使用system来执行复杂的shell命令
  5. Linux安装后的基本配置
  6. Android 弹出对话框Dialog充满屏幕宽度
  7. go1.6.2 linux/amd64 的一个bug: gcc: 无法识别的选项&lsquo;-no-pie&rsquo;
  8. [原]OpenGL基础教程(二)多边形绘制
  9. [IOS Block和delegate的对比]
  10. Windows 10触摸板手势
  11. 六.CSS浮动与清除
  12. Nginx Location配置总结及基础最佳实践
  13. Mysql int(11) 和 int(1)
  14. JAVA学习篇--Java类加载
  15. Redis笔记——技术点汇总
  16. 201521123060 《Java程序设计》第12周学习总结
  17. SWUST OJ(1038)
  18. UVa 11389 - The Bus Driver Problem 难度:0
  19. 创建第一次C语言程序
  20. css设置字体单行,多行超出省略号显示

热门文章

  1. 终于有人把云计算、大数据和 AI 讲明白了【深度好文】
  2. 『现学现忘』Git基础 — 25、git log命令参数详解
  3. Fuzzing101系列 Exercise 1 - Xpdf
  4. kali linux安装后乱码的解决方法
  5. extcon驱动及其在USB驱动中的应用
  6. nginx 源码安装配置详解(./configure)
  7. antdVue问题
  8. Vue基础篇之 插槽 slot
  9. 用HMS Core地图服务自定义地图样式,给你的应用制作专属个性化地图
  10. 解读ICDE&#39;22论文:基于鲁棒和可解释自编码器的无监督时间序列离群点检测算法