第七章 u3d中的定制特性

dll怎么用:

  就在vs里面生成,然后在使用的项目里引用包含以下这个dll就可以了

  至于unity,直接放进去

  注意:U3D目前最多支持.NET 3.5版本,所以生成DLL应该注意下版本问题

  (如果没有刷出来,就重启下vs。。。。。)

 

非托管代码的dll调用方法:

  http://www.ceeger.com/forum/read.php?tid=1097这里有比较详细的讲解。

 

Serializable特性

  就是序列化,在第十章会有详尽的介绍,这里简单写一个例子来说明下好了。

 

human.cs

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


[Serializable]
public enum Sex {
    male,female,unknow=10086
}
[Serializable]
public class Human {
    Sex __sex;
    string name;
    int age;
    //初始化一个人类
    public Human(Sex sex, string Name, int Age) {
        __sex = sex;
        name = Name;
        age = Age;
    }

    public override string ToString()
    {

        return "我的名字是:"+name+"  年龄:"+age+"  性别是:"+(__sex==Sex.male?"男性":"女性");
    }

}

testdll.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using myfirstdll;
using System.Runtime.InteropServices;

//系统IO
using System.IO;
//二进制格式化器
using System.Runtime.Serialization.Formatters.Binary;

public class testdll : MonoBehaviour {

    Human xiaoming; 

    // Use this for initialization
    void Start () {
        xiaoming = new Human(Sex.male, "小明", 22);
        FileStream f = new FileStream("xiaoming.dat", FileMode.Create,FileAccess.ReadWrite);
        BinaryFormatter bf = new BinaryFormatter();
        bf.Serialize(f, xiaoming);
        f.Close();

    }
    
    // Update is called once per frame
    void Update () {


    }

    void OnGUI() {
        if (GUI.Button(new Rect(0, 0, 300, 300), "反序列化")) {
            BinaryFormatter bf2 = new BinaryFormatter();
            FileStream f2 = new FileStream("xiaoming.dat", FileMode.Open, FileAccess.Read);
            Human human = bf2.Deserialize(f2) as Human;
            print(human);
            f2.Close();
        }


    }
}

在第十章会做详细的讲解

 

定制特性到底是什么呢?简单来说定制特性是一个类型的实例。Mono之所以能够跨平台其中原因之一是它符合CLS(公共语言规范)的要求,而根据公共语言规范的要求定制特性类必须直接或者间接从公共抽象类System.Attribue派生。

 

unity3d中最常用的定制特性:定制菜单

假如说想在Component菜单里加入一个项目,实现一个添加对应脚本组件的功能,只需要这样写:

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

[AddComponentMenu("TestMenu/Editor_Test")]
public class Editor_Test : MonoBehaviour {
    // Use this for initialization
    void Start () {
        print("this is Editor_Test");
    }
    
    // Update is called once per frame
    void Update () {
        
    }
}

U3D的一些更常用的特性是在UnityEditor这个命名空间里的

对于UnityEditor的特性,比如MenuItem特性,如果要使用它,就要using UnityEditor

MenuItem特性允许你添加菜单项目到主菜单和检视面板上下文菜单,并且这个特性会把所有的静态方法转变为菜单命令。也就是说这个属性是针对静态方法的。

  关于MenuItem特性的构造方法可以参考文档

  特别提醒的是,可以用多个静态方法来针对一个特性,比如一个方法用来控制是否可用,一个方法用来控制具体的行为。

  实例:

    [MenuItem("TestMenu/DoubleStaticFunction")]
    static void FuncitonBehave() {
        print("InstanceId is :" + Selection.activeGameObject.GetInstanceID());
    }
    [MenuItem("TestMenu/DoubleStaticFunction",true)]
    static bool FunctionAvalible() {
        return Selection.activeObject != null;
    }

  具体问题到时候用的时候去看API就好

 

关于其他的编辑器编辑功能,先不做介绍了,基本就是UnityEditor里面的内容了。

做一个提醒自己喝水和活动的编辑器窗口实例好了。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class Editor_WindowTest : EditorWindow {


   public    System.TimeSpan _RestDelatTime;
    System.DateTime _startTime;

    [MenuItem("Health/Drink&Monve")]
    static void ShowAsh()
    {
        Editor_WindowTest window = (Editor_WindowTest)Editor_WindowTest.GetWindow(typeof(Editor_WindowTest));
    }
    void Awake() {
        _startTime = System.DateTime.Now;
        //初始化休息时间
        _RestDelatTime = new System.TimeSpan(0, 30, 0);
    }

    void OnGUI() {
        System.DateTime _time=System.DateTime.Now;
        System.TimeSpan _timeSpan = _time - _startTime;
        GUILayout.Label("程序员健康系统");
        GUILayout.Label("你已经坐了:"+_timeSpan.Hours+"小时 "+_timeSpan.Minutes+"分钟 "+_timeSpan.Seconds+"");
        GUILayout.Label("休息的时间间隔:" + _RestDelatTime);

        if (_timeSpan.Hours > _RestDelatTime.Hours) {
            this.ShowNotification(new GUIContent("坐太久了!需要活动和喝水!"));
            _startTime = System.DateTime.Now;
        }
        else if ((_timeSpan.Hours == _RestDelatTime.Hours) && _timeSpan.Minutes >= _RestDelatTime.Minutes)
        {
            this.ShowNotification(new GUIContent("坐太久了!需要活动和喝水!"));
            _startTime = System.DateTime.Now;
        }
    }
    

}

(就不要吐槽写得烂了……能用就行。。。)

 

总结下:

  感觉这些东西还是挺有用的,能开发一些小但是实用的小工具。。。也可能会陷入“不务正业系列”,比如我的这个IDE:

(也许有一天Unity也会变成这样。。。想想就开心23333)

 

而且也能用一些工具来缓解开发的压力了,比如一个简单的GAL game的模板?或者是给那些愚蠢的策划用的开发工具?

日后会好好开发♂这个功能

 

posted @ 2017-06-13 21:03  -Ash  阅读(326)  评论(0编辑  收藏  举报