草庐IT

【Unity学习笔记】掌握MoneBehavior中的重要属性、方法

ElecSheep 2023-03-28 原文

一、重要属性

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("成功获取了");
        }
    }
}

有关【Unity学习笔记】掌握MoneBehavior中的重要属性、方法的更多相关文章

  1. ruby - 如何使用 Nokogiri 的 xpath 和 at_xpath 方法 - 2

    我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div

  2. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  3. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  4. ruby - 其他文件中的 Rake 任务 - 2

    我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时

  5. ruby-on-rails - Ruby net/ldap 模块中的内存泄漏 - 2

    作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代

  6. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  7. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  8. Ruby 方法() 方法 - 2

    我想了解Ruby方法methods()是如何工作的。我尝试使用“ruby方法”在Google上搜索,但这不是我需要的。我也看过ruby​​-doc.org,但我没有找到这种方法。你能详细解释一下它是如何工作的或者给我一个链接吗?更新我用methods()方法做了实验,得到了这样的结果:'labrat'代码classFirstdeffirst_instance_mymethodenddefself.first_class_mymethodendendclassSecond使用类#returnsavailablemethodslistforclassandancestorsputsSeco

  9. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  10. ruby-on-rails - 如果为空或不验证数值,则使属性默认为 0 - 2

    我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val

随机推荐