Unity Editor Scripting 2

来源:互联网 发布:拼音软件下载排行 编辑:程序博客网 时间:2024/06/06 01:53

1 CustomEditor 的例子

using UnityEngine;using System.Collections;public class LevelScript : MonoBehaviour {    public int experience;    public int Level    {        get { return experience / 750; }    }}
using UnityEngine;using System.Collections;using UnityEditor;[CustomEditor(typeof(LevelScript))]public class LevelScriptEditor : Editor {    public override void OnInspectorGUI()    {        LevelScript myTarget = (LevelScript)target;        myTarget.experience = EditorGUILayout.IntField("Experience", myTarget.experience);        EditorGUILayout.LabelField("Level", myTarget.Level.ToString());    }}

2 drag picture to folder

using UnityEngine;using UnityEditor;class MakeUIImage : AssetPostprocessor {    void OnPreprocessTexture () {        // Automatically convert any texture file with "GUIImages" in its file name into an uncompressed unchanged GUI Image.        if (assetPath.Contains("UI_Images") || assetPath.Contains("SpriteFonts") || assetPath.Contains("SpriteAtlases")) {            Debug.Log ("Importing new GUI Image!");            TextureImporter myTextureImporter  = (TextureImporter)assetImporter;            myTextureImporter.textureType = TextureImporterType.Advanced;            myTextureImporter.textureFormat = TextureImporterFormat.ARGB32;            myTextureImporter.convertToNormalmap = false;            myTextureImporter.maxTextureSize = 2048;            myTextureImporter.grayscaleToAlpha = false;            myTextureImporter.generateCubemap = TextureImporterGenerateCubemap.None;            myTextureImporter.npotScale = TextureImporterNPOTScale.None;            myTextureImporter.isReadable = true;            myTextureImporter.mipmapEnabled = false;//            myTextureImporter.borderMipmap = false;//            myTextureImporter.correctGamma = false;            myTextureImporter.mipmapFilter = TextureImporterMipFilter.BoxFilter;            myTextureImporter.fadeout = false;//            myTextureImporter.mipmapFadeDistanceStart;//            myTextureImporter.mipmapFadeDistanceEnd;            myTextureImporter.convertToNormalmap = false;//            myTextureImporter.normalmap;//            myTextureImporter.normalmapFilter;//            myTextureImporter.heightmapScale;            myTextureImporter.lightmap = false;            myTextureImporter.ClearPlatformTextureSettings("Web");            myTextureImporter.ClearPlatformTextureSettings("Standalone");            myTextureImporter.ClearPlatformTextureSettings("iPhone");        }    }}

3 scriptable wizard
public class SelectAllOfTag : ScriptableWizard

4 CustomPropertyDrawer

using UnityEngine;using UnityEditor;[CustomPropertyDrawer (typeof (ScaledCurve))]public class ScaledCurveDrawer : PropertyDrawer {    const int curveWidth = 50;    const float min = 0;    const float max = 1;    public override void OnGUI (Rect pos, SerializedProperty prop, GUIContent label) {        SerializedProperty scale = prop.FindPropertyRelative ("scale");        SerializedProperty curve = prop.FindPropertyRelative ("curve");        // Draw scale        EditorGUI.Slider (            new Rect (pos.x, pos.y, pos.width - curveWidth, pos.height),            scale, min, max, label);        // Draw curve        int indent = EditorGUI.indentLevel;        EditorGUI.indentLevel = 0;        EditorGUI.PropertyField (            new Rect (pos.width - curveWidth, pos.y, curveWidth, pos.height),            curve, GUIContent.none);        EditorGUI.indentLevel = indent;    }}
0 0
原创粉丝点击