unity

ooowl
  • unity
  • 游戏开发
About 5 min

unity

也不知道该放哪

在unity中,脚本是挂载在unity sense下的unity对象上才能执行的? 所有派生自MonoBehaviour的类都可以直接(拖拽)到unity对象上挂载

脚本名必须和类名一样,否则是挂载不上去的

use UnityEngine;
Debug.Log("I am a log"); // 还是常规的日志级别
在unity中使用日志,在unity中的Console窗口中显示,但是右上角有级别过滤,打印不出来的时候看看是不是手贱过滤了

驼峰命名:

int HpValue =1000; // 驼峰+语义,其实驼峰有很多种,C#的习惯来看这样比较好,需要可以再查
int iHpValue= 1000; // 带上了数据类型,有更多信息
char a='a'// char 是单引号,单字符

打印字符串的时候尽量format而不是直接拼接,因为字符串是不可变变量,更占内存

// 常用的前缀:
// str = string 
// ch= char
// i = int
// ll = longlong
// b or is =bool true false

C#的数组内存是线性连续的,且虽托管但是直接访问内存 C#的数组长度是整体长度,不是一维的长度,如果要取每个维度的长度,就需要getLength(n)

联合调试的时候首先要在Rider里面进行断点,然后unity里面也要运行程序才进入debug

unity设置个人偏好: play mode的全球变灰很难受直接选全白就不会有影响了,然后注意play的时候上面蓝色的播出箭头 search->play mode->play mode tint

unity中的钩子函数

using System.Collections;  
using System.Collections.Generic;  
using UnityEngine;  
using System;  
  
public class parc9_func : MonoBehaviour  
{   public int iHp = 1000; // MonoBehaviour类的public变量可以直接在unity中拖拽修改
    // Start is called before the first frame update  # 初始化的时候会调用一次 执行完会自动GC一次
    void Start()  
    { 
    }  
    // Update is called once per frame  # 每一帧都被调用
    void Update()  
    {            
    }
}

公开仓库open in new window

int a1 = 10;  
int a2 = 10;  

int a3 = a1++; // 先赋值再调用++所以是10
int a4 = ++a2; // 先调用++再赋值所以是11
Console.WriteLine(a3);  //10
Console.WriteLine(a4);  //11

唐老狮 BeaverJoe的个人空间open in new window

c#核心技术指南 深入解析c# C#图解教程(可以看第1-2章)

effective C# https://b23.tv/Bfuz4Kpopen in new window 从零开始做游戏1——用桌游验证游戏原型 https://b23.tv/HmoiKgLopen in new window 【聊聊买量游戏逻辑二次元凸起和海外市场SLG-哔哩哔哩】 https://b23.tv/yhzOVU0open in new windowBiosimulation的个人空间-Biosimulation个人主页-哔哩哔哩视频open in new window

使用base访问的是父类的方法,this查找的是当前类的属性

如果在父类中定义了一个函数,那么子类可以直接调用这个函数而无需重新定义。这种情况下,子类直接继承了父类的功能。然而,虚函数和重写在面向对象编程中起到关键作用,尤其在实现多态性和动态方法绑定(dynamic binding)方面。

什么是虚函数

虚函数(virtual function)是一种允许子类在继承父类函数的同时,重新定义其行为的机制。在父类中定义虚函数,然后在子类中重写(override),可以让子类提供自己的实现。

为什么需要虚函数和重写

虚函数和重写的关键作用是实现多态性,即在运行时能够根据对象的实际类型调用相应的方法。这在设计灵活的面向对象系统时非常重要。

动态方法绑定

在没有虚函数的情况下,编译器在编译时决定调用哪个方法,这叫做静态方法绑定(static binding)。即使子类定义了一个与父类同名的方法,也不会发生多态行为,调用仍然是基于编译时的类型。

而有了虚函数,编译器会根据实际对象的类型在运行时决定调用哪个方法。这叫做动态方法绑定(dynamic binding)或后期绑定(late binding)。

示例

public class BaseClass
{
    public void RegularMethod()
    {
        Console.WriteLine("Base class method");
    }

    public virtual void VirtualMethod()
    {
        Console.WriteLine("Base class virtual method");
    }
}

public class DerivedClass : BaseClass
{
    public void RegularMethod()
    {
        Console.WriteLine("Derived class regular method");
    }

    public override void VirtualMethod()
    {
        Console.WriteLine("Derived class overridden virtual method");
    }
}

在这个示例中,RegularMethod在子类中重写,但不是虚函数。这意味着如果使用父类引用访问子类对象,调用的仍然是父类的RegularMethod

VirtualMethod是虚函数,子类通过override重写了这个方法。如果使用父类引用访问子类对象,调用的将是子类的VirtualMethod

测试虚函数行为

BaseClass baseClass = new BaseClass();
DerivedClass derivedClass = new DerivedClass();

BaseClass baseRefToDerived = new DerivedClass();

// 静态方法绑定,调用父类的 RegularMethod
baseRefToDerived.RegularMethod();  // 输出 "Base class method"

// 动态方法绑定,调用子类的 VirtualMethod
baseRefToDerived.VirtualMethod();  // 输出 "Derived class overridden virtual method"

在这个示例中,baseRefToDerived是父类类型的引用,但指向子类实例。如果没有虚函数和重写,调用RegularMethodVirtualMethod会根据引用类型,而不是实际对象的类型。这会导致行为与预期不符。

结论

虚函数和重写在实现多态性方面至关重要。它们使得在运行时根据对象的实际类型调用正确的方法成为可能,而不依赖于编译时的静态绑定。通过使用虚函数,面向对象编程可以更灵活、更易于扩展。

Loading...