Unity in Machine Learning

小尝试

  1. 在UnityHub里,新建一个项目

  2. 建一个Object“Marker”
    (a)右单击“Hierarchy”栏的空白处
    (b)从菜单里选择"Create Empty",命名为“TheSimplestGame”
    (c) 将鼠标移动到“TheSimplestGame”上右单击,从菜单里选择“3D Object”>“Cube”
    (d)在有半边IDE的"Project"栏中“Assets”里面创建一个新的文件夹,命名为"Scripts".
    (e)将鼠标移动到"Scripts"上右单击,从菜单里选择“Create”>“C# Scripts”
    (f)将新建的C# Script拖至"Marker",成为其Child 文件。查看关联是否成功:左单击"Marker",在“Inspector”里会找到Script相关的“Component”
    在这里插入图片描述

  3. 现在修改Main.cs 中的代码,就可以运行Unity了(注意是在Unity里面运行,而不是在vs code里运行):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class main : MonoBehaviour
{
    // Start is called before the first frame update
    void Start(){}
    // Update is called once per frame
    void Update(){}
    void OnDisable() {Debug.Log("Bye");}
    void OnEnable() { Debug.Log("Hi")}
}

自定义object 的属性

添加两个public variables,这两个属性就会显示在Unity里。只有当属性带有“public”才行。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class main : MonoBehaviour
{
	public string gameName = "夜游神";
    public int age = 26;
    // Start is called before the first frame update
    void Start(){}
    // Update is called once per frame
    void Update(){}
    void OnDisable() {Debug.Log("Bye");}
    void OnEnable() { Debug.Log("Hi")}
}

在这里插入图片描述
这样以后,属性就可以直接在Unity里改,而C# 里的当作是一个示例。

C# 访问修饰符

public:同一程序集中的任何其他代码或引用该程序集的其他程序集都可以访问该类型或成员。 某一类型的公共成员的可访问性水平由该类型本身的可访问性级别控制。

private:只有同一 class 或 struct 中的代码可以访问该类型或成员。

protected:只有同一 class 或者从该 class 派生的 class 中的代码可以访问该类型或成员。

internal:同一程序集中的任何代码都可以访问该类型或成员,但其他程序集中的代码不可以。

protected internal:该类型或成员可由对其进行声明的程序集或另一程序集中的派生 class 中的任何代码访问。

private protected:该类型或成员可以通过从 class 派生的类型访问,这些类型在其包含程序集中进行声明。
在这里插入图片描述

C# NameSpace

player.cs

using System;
namespace Beginning.CSharp
{
    public struct Player { 
    	public string name;
    }
}

main.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class main : MonoBehaviour
{
	public string gameName = "夜游神";
    public int age = 26;
    // Start is called before the first frame update
    void Start(){
    	player = new Beginning.CSharp.Player();
    	player.name = "007";
    }
    // Update is called once per frame
    void Update(){}
    void OnDisable() {Debug.Log("Bye");}
    void OnEnable() { Debug.Log("Hi")}
}

Object运动

例1

  • 关联后的Object,通过“gameObject”去更改对应属性(也可以不用,在下一个例子)。
  • 关联的Object可以有用Vector2D, Vector3D重写坐标.(Update method里 )
  • 在关联Object的Unity“Inspector”栏中添加新的components,比如例子中的“Rigidbody”,可以在Script中引用和运用。
public class main : MonoBehaviour
{
    public string gameName = "夜游神";
    public int age = 26;
    Beginning.CSharp.Player player;

    public Vector2 cord2D;
    public Vector3 cord3D;
    public float speed = 10f;

    // Start is called before the first frame update
    void Start()
    {
        Rigidbody rb = GetComponent<Rigidbody>();
        if (rb != null){rb.AddForce(Vector3.up * 500f);}    
    }
    // Update is called once per frame
    void Update(){}
    void OnDisable() {Debug.Log("Bye");}
    void OnEnable() {Debug.Log("Hi");}

例2

  1. 不用“gameObject”更改,直接使用“Inspector”栏中的属性“transform”
  2. 以下例子可以让方块移动起来(运行unity后,按上下左右键)
public class main : MonoBehaviour
{
    public string gameName = "夜游神";
    public int age = 26;

    public Vector2 cord2D;
    public Vector3 cord3D;
    public float speed = 10f;

    // Start is called before the first frame update
    void Start(){}
    // Update is called once per frame
    void Update(){
transform.Translate(Input.GetAxis("Horizontal")*Time.deltaTime*speed, 0,Input.GetAxis("Vertical") * Time.deltaTime * speed);
    }
    void OnDisable() {Debug.Log("Bye");}
    void OnEnable() {Debug.Log("Hi");}

Python检测 + C#

详细Implementation 在: Python C# 通信四种方法
个人亲测了两种路子:第一种是C#里调terminal的Python 执行命令行,然后C#将terminal的print结果获取进来Unity。第二种路子用的是socket,使用的是TCP的通信方式,同时开启Python和C#脚本互相通信。
以下比较这两者的优缺点:

第1种

参考链接:

  1. C#里用: Process
  2. Python 里确保写成module friendly的

优点:
- 直接在C#里写Python 代码
- 可以C# Python互传参数
缺点:
- 需要使用pip install module的方式,将你machine learning的代码打包进你无论是Python3.6,Python3.7 的 包内。
- 通信速度慢

第2种

参考链接:

  1. C# 里用:TCPListener
  2. Python 里用 socket
    优点:
    - 速度快
    - 可以C# Python互传参数
    缺点:
    - 需要一点点Network的知识(对于IT从业者来说,这不算缺点)

注意:
由于Unity MonoBehaviour 的update()方法和TCP传接的运行速度不匹配,会有造成信息顺序不同的风险。个人建议使用Queue 将TCP从Python得到的数据存入,然后MonoBehaviour 的部分用Queue.dequeue的方式将信息顺序获取。

Object 运动方式

参考一

Unity 的运动方式仅以上参考。
个人在做Project时,Unity MonoBehaviourupdate()的方法只能更新一帧画面,不太适用于在里面写一次性动作。最好在Python里,写好动作。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值