C# 中级教程 From Unity 社区 笔记

这篇博客是关于C#在Unity中的中级教程,涵盖了属性的概念及其作为域扩展的功能,如何使用访问器进行读写操作。此外,介绍了单例模式的设计原则和实现,强调其在限制对象实例数量方面的作用。还讨论了不同脚本间的调用方法,如使用static函数和SendMessage。最后提到了继承、多态、成员隐藏和覆盖,以及Unity中的协程和事件系统。
摘要由CSDN通过智能技术生成

C# 中级教程 From Unity 社区

最近b站unity官方发了个C#的教程,一开始以位简简单单,后来发现自己对于OOP还是有很多地方没掌握好,特此记录

  1. Properties

    属性(Property) 是类(class)、结构(structure)和接口(interface)的命名(named)成员。类或结构中的成员变量或方法称为 域(Field)。属性(Property)是域(Field)的扩展,且可使用相同的语法来访问。它们使用 访问器(accessors) 让私有域的值可被读写或操作。

    属性(Property)不会确定存储位置。相反,它们具有可读写或计算它们值的 访问器(accessors)

    例如,有一个名为 Student 的类,带有 age、name 和 code 的私有域。我们不能在类的范围以外直接访问这些域,但是我们可以拥有访问这些私有域的属性。

    快速构建: 输入prop 然后按Tab

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    namespace Test_Fox
    {
        public partial class Test1 : MonoBehaviour
        {
            public int a = 2;
            private int experience;
    
            public int Experience { get; set; }
    
    
            private void Start()
            {//同类
                
                Debug.Log(a);
                Experience = 10;
                Debug.Log(Experience);
    
            }
        }
         public partial class Test3 : MonoBehaviour
        {
            //不同类(同文件)
            // private void Start()
            // {
            //     Test1 test = new Test1();
            //     Debug.Log(test.a);
            //     test.Experience = 10;
            //     Debug.Log(test.Experience);
    
            // }
        }
    }
    
    //若在不同脚本中
    //GameObject.Find("脚本所在物体名").GetComponent<脚本名>().函数名();此种方法只可以调用public类型函数。
    //GameObject.Find("脚本所在物体名").SendMessage("函数名");  此种方法可以调用public和private类型函数。
    
  2. 单例模式(Singleton Pattern)

    是最简单的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
    这种模式涉及到一个单一的类,该类负责创建自己的对象,同时确保只有单个对象被创建。这个类提供了一种访问其唯一的对象的方式,可以直接访问,不需要实例化该类的对象。

    注意:
    1、单例类只能有一个实例。
    2、单例类必须自己创建自己的唯一实例。
    3、单例类必须给所有其他对象提供这一实例。

  3. 不同脚本之间调用

  4. 被调用脚本函数为static类型,调用时直接用 脚本名.函数名()

  5. GameObject.Find(“object”).SendMessage(“method”); //能调用public和private类型函数

  6. GameObject.Find(“object”).GetComponent().SendMessage(“method”,null); //可以添加参数

  7. GameObject.Find(“object”).GetComponent().函数名(); //只能调用public类型函数

//脚本1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Test_Fox
{
    public partial class Test1 : MonoBehaviour
    {
        public int a = 2;
        private int experience;

        public int Experience { get; set; }

        public static void Hello(){
            Debug.Log("hello,Test2");
        }
        public void Goodbye(){
            Debug.Log("Goodbye,Test2");
        }
        public void SayHi(string a){
            Debug.Log("Hi,"+a);
        }
        private void Start()
        {
            
            Debug.Log(a);
            Experience = 10;
            Debug.Log(Experience);

        }
    }
     public partial class Test3 : MonoBehaviour
    {
        // private void Start()
        // {
        //     Test1 test = new Test1();
        //     Debug.Log(test.a);
        //     test.Experience = 10;
        //     Debug.Log(test.Experience);

        // }
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Test_Fox;

public class Test2 : MonoBehaviour
{
    private string myName = "fox";
    //Test2 test = new Test2();
    private void Start()
    {
        Test1.Hello();//静态类
        GameObject.Find("Main Camera").SendMessage("Goodbye");//要绑定
        GameObject.Find("Main Camera").GetComponent<Test1>().SendMessage("SayHi",myName);  //可以添加参数
        GameObject.Find("Main Camera").GetComponent<Test1>().Goodbye();//只能调用public类型函数
        GameObject.Find("Main Camera").GetComponent<Test1>().SayHi(myName);//只能调用public类型函数
    }
}

  1. 继承,多态,成员隐藏,覆盖

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class Fruit : MonoBehaviour
    {
        private bool isEat;
        private int cost;
        private int nums;
        private string itName = "f";
    
        // public Fruit(string n)
        // {
        //     itName = n;
        // }
    
        public void printfmyname()
        {
            Debug.Log(itName);
        }
    
        public virtual void printf1()
        {
            Debug.Log("1");
        }
    
    
        private void Start()
        {
            Fruit Rect = new Fruit();
            Rect.printf1();
        }
    }
    
    
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class Apple : Fruit
    {
        private string n = "a";
        public Apple(string n){
    
        }
        public void sonsay(){
            Debug.Log("i am a child");
        }
        //new 待表重写
        new public void printfmyname(){
            Debug.Log("a");
        }
        //需要父类标记为virtual
        public override void printf1(){
            base.printf1();//保留父类这个方法的code
            Debug.Log("2");
        }
        private void Start() {
            Apple a = new Apple("a");
            a.printfmyname();
            a.printf1();
            Fruit f = new Apple("b");//向上转型
            f.printfmyname(); 
            f.printf1();
        }
    
    }
    
    
  2. 协程(看成一个优先级较低(可随时中断)的小线程)

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class CoroutinesExample : MonoBehaviour
    {
        public float smoothing = 10f;
        public Transform target;
    
        private void Start()
        {
            Debug.Log("1");
            Debug.Log("12");
            StartCoroutine(MyCoroutine(target));
            //MyCoroutine1(target);
            Debug.Log("123");
        }
        private void Update()
        {
            //MyCoroutine1(target);
        }
        IEnumerator MyCoroutine(Transform target)
        {
            while (Vector3.Distance(transform.position, target.position) > 0.05f)
            {
                transform.position = Vector3.Lerp(transform.position, target.position, smoothing * Time.deltaTime);
                yield return null;
            }
    
            print("Reached the target.");
    
            yield return new WaitForSeconds(3f);
    
            print("Go");
        }
        void MyCoroutine1(Transform target)
        {
            transform.position = Vector3.Lerp(transform.position, target.position, smoothing * Time.deltaTime);
    
            if (Vector3.Distance(transform.position, target.position) < 0.05f)
            {
                print("Reached the target.");
    
                print("Go");
            }
    
        }
    }
    
    
  3. 特性

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    //不用点运行也有效果,永久性的
    [ExecuteInEditMode]
    public class SpinScript : MonoBehaviour
    {
       [Range(-100,100)]
       public int speed = 0;
    
       private void Update() {
           transform.Rotate(new Vector3(0,speed*Time.deltaTime,0));
       }
    }
    
    
  4. 事件(太多了) 略

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值