笔记 - Unity自定义文件加载解析

https://docs.unity3d.com/2019.3/Documentation/ScriptReference/Experimental.AssetImporters.AssetImporterEditor.html

using System;
using System.IO;
using UnityEditor;
using UnityEditor.Experimental.AssetImporters;
using UnityEngine;
using Object = UnityEngine.Object;

[CustomEditor(typeof(BooleanImporter))]
[CanEditMultipleObjects]
public class BooleanImporterEditor : ScriptedImporterEditor
{
    // Property to show in the custom OnInspectorGUI.
    SerializedProperty m_BooleanProperty;

    // override extraDataType to return the type that will be used in the Editor.
    protected override Type extraDataType => typeof(BooleanClass);

    // override InitializeExtraDataInstance to set up the data.
    protected override void InitializeExtraDataInstance(Object extraTarget, int targetIndex)
    {
        var boolean = (BooleanClass)extraTarget;
        // Read the boolean value from the text file and fill the extraTarget object with the data.
        string fileContent = File.ReadAllText(((AssetImporter)targets[targetIndex]).assetPath);
        if (!bool.TryParse(fileContent, out boolean.boolean))
        {
            boolean.boolean = false;
        }
    }

    protected override void Apply()
    {
        base.Apply();
        // After the Importer is applied, rewrite the file with the custom value.
        for (int i = 0; i < targets.Length; i++)
        {
            string path = ((AssetImporter)targets[i]).assetPath;
            File.WriteAllText(path, ((BooleanClass)extraDataTargets[i]).boolean.ToString());
        }
    }

    public override void OnEnable()
    {
        base.OnEnable();
        // In OnEnable, retrieve the importerUserSerializedObject property and store it.
        m_BooleanProperty = extraDataSerializedObject.FindProperty("boolean");
    }

    public override void OnInspectorGUI()
    {
        // Note: you don't need to call serializedObject.Update or serializedObject.ApplyModifiedProperties
        // because you are not changing the target (serializedObject) itself.

        // Update the importerUserSerializedObject in case it has been changed outside the Inspector.
        extraDataSerializedObject.Update();

        // Draw the boolean property.
        EditorGUILayout.PropertyField(m_BooleanProperty);

        // Apply the changes so Undo/Redo is working.
        extraDataSerializedObject.ApplyModifiedProperties();

        // Call ApplyRevertGUI to show Apply and Revert buttons.
        ApplyRevertGUI();
    }
}

public class BooleanClass : ScriptableObject
{
    public bool boolean;
}

[ScriptedImporter(0, ".boolean")]
public class BooleanImporter : ScriptedImporter
{
    public override void OnImportAsset(AssetImportContext ctx)
    {
        string fileContent = File.ReadAllText(ctx.assetPath);
        var booleanObj = ObjectFactory.CreateInstance<BooleanClass>();
        if (!bool.TryParse(fileContent, out booleanObj.boolean))
        {
            booleanObj.boolean = false;
        }
        ctx.AddObjectToAsset("main", booleanObj);
        ctx.SetMainObject(booleanObj);
        Debug.Log("Imported Boolean value: " + booleanObj.boolean);
    }
}

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值