第5节、类

5.1 C++中类的实例化

​ 在C++中实例化一个类不需要用new。也可以用new,但是区别是:不使用new,开辟栈空间,使用new开辟的空间为堆空间。

5.2 构造函数和析构函数、拷贝构造函数

5.2.1.析构函数

​ 析构函数是C++内释放的时候调用的函数。

构造函数语法:类名(){}

  1. 构造函数,没有返回值也不写void
Read More

L02外挂式开发Editor

1.基本结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using UnityEngine;
//步骤 1:引入编辑器的命名空间,检视器属于编辑器开发范畴 using UnityEditor;
[CustomEditor (typeof (Player)]//步骤 3:将编辑器开发脚本与需要编辑的组件脚本建立外挂关联关系/外挂脚本因为存储在 Editor 目录下,所以不会被打入最终的游戏包/不继承自 Mono,而是继承自 Editor
public class PlayerEditor: Editor//步骤 2:继承 Editor 类,使用编辑器相关的成员变量和生命周期函数
{
//获得到需要编辑显示的组件
private Player _Component;

//步骤 4:需要在当前的外挂脚本中,获得需要被扩展的 P1ayer 组件对象
//当关联组件所在对象被选中或组件被添加时,调用
private void OnEnable ()
{
//Debug. Log ("enable");
//步骤 5:获取 P1ayer 组件对象
_Component = target as Player;
}

//当关联组件所在对象被取消或组件被移除时,调用
private void OnDisable ()
{
//Debug. Log ("disable");
_Component = null;
}

//用于绘制检视面板的生命周期函数
public override void OnInspectorGUI ()
{

}
}

2.绘制GUI 基本数据类型

1.标题显示

1
EditorGUILayout.LabelField("人物相关属性");

2.文本

Read More

L04.练习

1.OnSceneGUI

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//有点类似前期Update函数,发送射线
//当选中关联的脚本挂载的物体
//当鼠标在Scene视图下发生变化时,执行该方法,比如鼠标移动,比如鼠标的点击
void OnSceneGUI()
{

if (!isEditor)//非编辑状态下不能生成路点
{
return;
}

//当鼠标按下左键时发射一条射线
//非运行时,使用Event类
//Event.current.button 判断鼠标是哪个按键的(0是鼠标左键)
//Event.current.type 判断鼠标的事件方式的(鼠标按下)
if (Event.current.button == 0 && Event.current.type == EventType.MouseDown)
{
//从鼠标的位置需要发射射线了
//因为是从Scene视图下发射射线,跟场景中的摄像机并没有关系,所以不能使用相机发射射线的方法
//从编辑器GUI中的一个点向世界定义一条射线, 参数一般都是鼠标的坐标
Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
if (Physics.Raycast(ray, out hit, 100, 1 << 31))
{
//需要在检测到的点实例化,路点
InstancePathNode(hit.point + Vector3.up * 0.1f);
}

}
}

2.Event.current.button

1
Event.current.button 判断鼠标是哪个按键的(0是鼠标左键)

Read More

L03.菜单栏开发

1.点击顶部菜单栏执行方法

1
2
3
4
5
6
7
8
9
10
11
//顶部菜单类
public class Menu
{
//在顶部显示"工具"菜单,下方有"导出AB资源包",点击执行函数
[MenuItem("工具/导出AB资源包")]
static void BuildAB()
{
//Debug.Log("导出AB资源包");
Debug.Log(Application.persistentDataPath);
}
}

2.点击顶部菜单栏创建窗口

1.绘制窗口

1
2
3
4
5
6
7
8
9
10
public class PopWindow : EditorWindow
{
[MenuItem("工具/创建窗口")]
static void OpenWindow()
{
PopWindow window = GetWindow<PopWindow>(false, "弹窗标题", true);
window.minSize = new Vector2(40, 30);
window.minSize = new Vector2(80, 60);
}
}

Read More

L04.实战 /images/wallhaven-7ple5y.jpg

1.lua实现生命周期

Bootstrap.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;

//lua生命周期
public delegate void LifeCycle();

//将Lua与Lua核心表对应的结构体
[GCOptimize]
public struct LuaBootstrap
{
public LifeCycle Start;
public LifeCycle Update;
public LifeCycle OnDestroy;
}

public class Bootstrap : MonoBehaviour
{
//Lua的核心table
private LuaBootstrap _Bootstrap;

//调用起Lua代码
void Start()
{
//防止切换场景时,脚本对象丢失
DontDestroyOnLoad(gameObject);

xLuaEnv.Instance.DoString("require('Bootstrap')");

//将Lua的核心table,导入到C#端
_Bootstrap = xLuaEnv.Instance.Global.Get<LuaBootstrap>("Bootstrap");
//lua start
_Bootstrap.Start();
}

void Update()
{
//luaupdate
_Bootstrap.Update();
}

//释放Lua的代码
void OnDestroy()
{
//lua OnDestroy
_Bootstrap.OnDestroy();

//释放Lua环境前,需要将导出到C#的Lua回调函数进行释放
_Bootstrap.Start = null;
_Bootstrap.Update = null;
_Bootstrap.OnDestroy = null;

xLuaEnv.Instance.Free();
}
}

Bootstrap.lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Bootstrap = {}

--核心table,存储所有的控制器
Bootstrap.Controllers = {}

Bootstrap.Start = function()
Bootstrap.LoadPage("MainMenuController")
end

Bootstrap.Update = function()
print("Lua Update")
end

Bootstrap.OnDestroy = function()
print("Lua OnDestroy")
end

Read More

L03 CSharp 调用 xLua

**1.C#调用Lua变量 **

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void Start()
{
xLuaEnv.Instance.DoString("return require('L2C/CsharpCallVariable')");
//LuaEnv 提供了个成员变量 Globa1,它可以用于 C#获取 Lua 的全局变量
//Global 的数据类型是 c#实现的 LuaTable.LuaTable 是×Lua 实现的 c#和 Lua 中表对应的数据结构
//xLua 会将 Lua 中的全局变量以 Tab1e 的方式全部存储在 G1obal 中
//通过运行环境,导出全局变量,类型是 LuaTable
//LuaTab1e 是 c#的数据对像,用于和 Lua 中的全局变量存储的 tablej 对应
LuaTable g = xLuaEnv.Instance.Global;

//从 Lua 中,将全局变量提取出来/参数:Lua 中全局变量的名称
//类型:Lua 中全局变量的名称所对应的类型/返回值:变量的值
int num = g.Get<int>("num");
float rate = g.Get<float>("rate");
bool isWoman = g.Get<bool>("isWoman");
string name = g.Get<string>("name");
Debug.Log("数字:" + num);
Debug.Log("浮点数:" + nun;
Debug.Log("布尔:" + num);
Debug.Log("字符串:" + num);
}
1
2
3
4
5
//隐性做了{num=108.rate=99.99.isWoman=fa1se.name="admin")
num 100
rate = 99.99
isWoman = false
name = "admin"

**2.C#调用Lua函数 **

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
func1 function()
print("这是 Lua 中的 func1")
end

func2 function(name)
print("这是 Lua 中的 func2,参数是:" ..name)
end

func3 function()
return "这是 Lua 中的 func3"
end

func4 function()
return "这是 Lua 中的 func4",100
end

Read More