Unity中文教程网中Unity教程 C#中级编程

这篇博客详细介绍了Unity引擎中C#编程的一些中级概念,包括属性的创建和使用,三元运算符,静态变量和方法,方法重载,泛型以及继承。通过示例代码展示了如何在游戏开发中应用这些技巧,如使用属性简化变量操作,利用静态成员跟踪对象数量,以及如何通过方法重载和泛型实现更灵活的函数和类设计。此外,还阐述了继承的概念,演示了如何从基类继承并自定义子类的行为。
摘要由CSDN通过智能技术生成

 

一、目的

1、想知道:Unity中文教程网中Unity教程 C#中级编程

https://learn.u3d.cn/tutorial/intermediate-gameplay-scripting#603f154d084785002045570e

 

二、参考

1、Unity教程 C#中级编程

https://learn.u3d.cn/tutorial/intermediate-gameplay-scripting#603f154d084785002045570e

  • 总结:good:很不错的Unity教程网站

 

三、操作+内容

1、创建属性

Player

using UnityEngine;
using System.Collections;

public class Player
{
    //成员变量可以称为
    //字段。
    private int experience;

    //Experience 是一个基本属性
    public int Experience
    {
        get
        {
            //其他一些代码
            return experience;
        }
        set
        {
            //其他一些代码
            experience = value;
        }
    }

    //Level 是一个将经验值自动转换为
    //玩家等级的属性
    public int Level
    {
        get
        {
            return experience / 1000;
        }
        set
        {
            experience = value * 1000;
        }
    }

    //这是一个自动实现的属性的
    //示例
    public int Health{ get; set;}
}

 

using UnityEngine;
using System.Collections;

public class Game : MonoBehaviour 
{
    void Start () 
    {
        Player myPlayer = new Player();

        //属性可以像变量一样使用
        myPlayer.Experience = 5;
        int x = myPlayer.Experience;
    }
}
  • 总结:知道了变量当作属性使用,使用set、get

 

2.三元运算符

using UnityEngine;
using System.Collections;

public class TernaryOperator : MonoBehaviour 
{
    void Start () 
    {
        int health = 10;
        string message;

        //这是一个三元运算的示例,其中根据
        //变量“health”选择一条消息。
        message = health > 0 ? "Player is Alive" : "Player is Dead";
    }
}
  • 总结:平时代码少写点if else了

 

3.静态

方法一:

Enemy

using UnityEngine;
using System.Collections;

public class Enemy
{
    //静态变量是在类的所有实例之间
    //共享的变量。
    public static int enemyCount = 0;

    public Enemy()
    {
        //通过递增静态变量了解
        //已创建此类的多少个对象。
        enemyCount++;
    }
}
  • 总结:静态变量是在类的所有实例之间共享的变量。       原来静态还可以这样;
  • 总结:public Enemy()    直接在构造函数中,增加了静态变量,实现数量自增;

Game

using UnityEngine;
using System.Collections;

public class Game
{
    void Start () 
    {
        Enemy enemy1 = new Enemy();
        Enemy enemy2 = new Enemy();
        Enemy enemy3 = new Enemy();

        //可以使用类名和点运算符
        //来访问静态变量。
        int x = Enemy.enemyCount;
    }
}

 

方法二:

Player

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour 
{
    //静态变量是在类的所有实例之间
    //共享的变量。
    public static int playerCount = 0;

    void Start()
    {
        //通过递增静态变量了解
         //已创建此类的多少个对象。
        playerCount++;
    }
}

PlayerManager

using UnityEngine;
using System.Collections;

public class PlayerManager : MonoBehaviour 
{
    void Start()
    {
        //可以使用类名和点运算符
        //来访问静态变量。
        int x = Player.playerCount;
    }
}
  • 总结:全局静态变量

方法三

Utilities

using UnityEngine;
using System.Collections;

public static class Utilities 
{
    //可以在没有类对象的情况下调用
    //静态方法。请注意,静态方法无法访问
    //非静态成员变量。
    public static int Add(int num1, int num2)
    {
        return num1 + num2;
    }
}

UtilitiesExample

using UnityEngine;
using System.Collections;

public class UtilitiesExample : MonoBehaviour 
{
    void Start()
    {
        //可以使用类名和点运算符
        //来访问静态方法。
        int x = Utilities.Add (5, 6);
    }
}
  • 总结:静态类,整个工程都能调用了
  • 总结:可以在没有类对象的情况下调用 静态方法。请注意,静态方法无法访问 非静态成员变量。

 

4.方法重载

SomeClass

using UnityEngine;
using System.Collections;

public class SomeClass
{
    //第一个 Add 方法的签名为
    //“Add(int, int)”。该签名必须具有唯一性。
    public int Add(int num1, int num2)
    {
        return num1 + num2;
    }

    //第二个 Add 方法的签名为
    //“Add(string, string)”。同样,该签名必须具有唯一性。
    public string Add(string str1, string str2)
    {
        return str1 + str2;
    }
}

SomeOtherClass

using UnityEngine;
using System.Collections;

public class SomeOtherClass : MonoBehaviour 
{
    void Start () 
    {
        SomeClass myClass = new SomeClass();

        //具体调用的 Add 方法将取决于
        //传入的参数。
        myClass.Add (1, 2);
        myClass.Add ("Hello ", "World");
    }
}
  • 总结:good:这个我已经会了,函数名字一样,参数不同

5.通用

方法一

SomeClass

using UnityEngine;
using System.Collections;

public class SomeClass 
{
    //这是一个通用方法。注意通用
    //类型“T”。该“T”将在运行时替换为
    //实际类型。
    public T GenericMethod<T>(T param)
    {
        return param;
    }
}

SomeOtherClass

using UnityEngine;
using System.Collections;

public class SomeOtherClass : MonoBehaviour 
{
    void Start () 
    {
        SomeClass myClass = new SomeClass();

        //为了使用此方法,必须
        //告诉此方法用什么类型替换
        //“T”。
        myClass.GenericMethod<int>(5);
    }
}
  • 总结:好像也叫做模板类,
  • 总结:public 同一个数据类型的返回值  函数名字<同一个数据类型的>(同一个数据类型的 参数)

方法二

GenericClass

using UnityEngine;
using System.Collections;

//这是一个通用类。注意通用类型“T”。
//“T”将被替换为实际类型,同样,
//该类中使用的“T”类型实例也将被替换。
public class GenericClass <T>
{
    T item;

    public void UpdateItem(T newItem)
    {
        item = newItem;
    }
}

GenericClassExample

using UnityEngine;
using System.Collections;

public class GenericClassExample : MonoBehaviour 
{
    void Start () 
    {        
        //为了创建通用类的对象,必须
        //指定希望该类具有的类型。
        GenericClass<int> myClass = new GenericClass<int>();

        myClass.UpdateItem(5);
    }
}
  • 总结:public class GenericClass <T>      个人觉得:提前给类一个数据类型,类似C++中的初始化列表赋值,然后将这个数据类型使用在属性、函数参数类型中
  • 总结:

6.继承

Fruit 类

using UnityEngine;
using System.Collections;

//这是基类,
//也称为父类。
public class Fruit 
{
    public string color;

    //这是 Fruit 类的第一个构造函数,
    //不会被任何派生类继承。
    public Fruit()
    {
        color = "orange";
        Debug.Log("1st Fruit Constructor Called");
    }

    //这是 Fruit 类的第二个构造函数,
    //不会被任何派生类继承。
    public Fruit(string newColor)
    {
        color = newColor;
        Debug.Log("2nd Fruit Constructor Called");
    }

    public void Chop()
    {
        Debug.Log("The " + color + " fruit has been chopped.");        
    }

    public void SayHello()
    {
        Debug.Log("Hello, I am a fruit.");
    }
}

Apple 类

using UnityEngine;
using System.Collections;

//这是派生类,
//也称为子类。
public class Apple : Fruit 
{
    //这是 Apple 类的第一个构造函数。
    //它立即调用父构造函数,甚至
    //在它运行之前调用。
    public Apple()
    {
        //注意 Apple 如何访问公共变量 color,
        //该变量是父 Fruit 类的一部分。
        color = "red";
        Debug.Log("1st Apple Constructor Called");
    }

    //这是 Apple 类的第二个构造函数。
    //它使用“base”关键字指定
    //要调用哪个父构造函数。
    public Apple(string newColor) : base(newColor)
    {
        //请注意,该构造函数不会设置 color,
        //因为基构造函数会设置作为参数
        //传递的 color。
        Debug.Log("2nd Apple Constructor Called");
    }
}
  • 总结:public Apple(string newColor) : base(newColor)     base这里指的就是Fruit类,并且低矮用Fruit(string newColor)

 

FruitSalad 类

using UnityEngine;
using System.Collections;

public class FruitSalad : MonoBehaviour 
{
    void Start () 
    {
        //让我们用默认构造函数
        //来说明继承。
        Debug.Log("Creating the fruit");
        Fruit myFruit = new Fruit();
        Debug.Log("Creating the apple");
        Apple myApple = new Apple();

        //调用 Fruit 类的方法。
        myFruit.SayHello();
        myFruit.Chop();

        //调用 Apple 类的方法。
        //注意 Apple 类如何访问
        //Fruit 类的所有公共方法。
        myApple.SayHello();
        myApple.Chop();

        //现在,让我们用读取字符串的
        //构造函数来说明继承。
        Debug.Log("Creating the fruit");
        myFruit = new Fruit("yellow");
        Debug.Log("Creating the apple");
        myApple = new Apple("green");

        //调用 Fruit 类的方法。
        myFruit.SayHello();
        myFruit.Chop();

        //调用 Apple 类的方法。
        //注意 Apple 类如何访问
        //Fruit 类的所有公共方法。
        myApple.SayHello();
        myApple.Chop();
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值