基本匯入商

假設你有一個要為其建立匯入程式的自定義檔案。它可能是 .xls 檔案或其他什麼。在這種情況下,我們將使用 JSON 檔案,因為它很容易,但我們將選擇一個自定義擴充套件,以便輕鬆判斷哪些檔案是我們的?

我們假設 JSON 檔案的格式是

{
  "someValue": 123,
  "someOtherValue": 456.297,
  "someBoolValue": true,
  "someStringValue": "this is a string",
}

讓我們暫時將其儲存為資產之外的某個地方。

接下來只為資料建立一個帶有自定義類的 MonoBehaviour。自定義類只是為了方便反序列化 JSON。你不必使用自定義類,但它會縮短此示例。我們將在 TestData.cs 中儲存這個

using UnityEngine;
using System.Collections;

public class TestData : MonoBehaviour {

    [System.Serializable]
    public class Data {
        public int someValue = 0;
        public float someOtherValue = 0.0f;
        public bool someBoolValue = false;
        public string someStringValue = "";
    }

    public Data data = new Data();
}

如果你要將該指令碼手動新增到 GameObject,你會看到類似的內容

StackOverflow 文件

接下來在 Assets 下的某個地方製作一個 Editor 資料夾。我可以在任何級別。在 Editor 資料夾中建立一個 TestDataAssetPostprocessor.cs 檔案並將其放入其中。

using UnityEditor;
using UnityEngine;
using System.Collections;

public class TestDataAssetPostprocessor : AssetPostprocessor
{
    const string s_extension = ".test";

    // NOTE: Paths start with "Assets/"
    static bool IsFileWeCareAbout(string path)
    {
        return System.IO.Path.GetExtension(path).Equals(
           s_extension, 
           System.StringComparison.Ordinal);
    }

    static void HandleAddedOrChangedFile(string path)
    {
        string text = System.IO.File.ReadAllText(path);
        // should we check for error if the file can't be parsed?
        TestData.Data newData = JsonUtility.FromJson<TestData.Data>(text);

        string prefabPath = path + ".prefab";
        // Get the existing prefab 
        GameObject existingPrefab = 
            AssetDatabase.LoadAssetAtPath(prefabPath, typeof(Object)) as GameObject;
        if (!existingPrefab)
        {
            // If no prefab exists make one
            GameObject newGameObject = new GameObject();
            newGameObject.AddComponent<TestData>();
            PrefabUtility.CreatePrefab(prefabPath, 
                                       newGameObject,
                                       ReplacePrefabOptions.Default);
            GameObject.DestroyImmediate(newGameObject);
            existingPrefab = 
                AssetDatabase.LoadAssetAtPath(prefabPath, typeof(Object)) as GameObject;
        }

        TestData testData = existingPrefab.GetComponent<TestData>();
        if (testData != null)
        {
            testData.data = newData;
            EditorUtility.SetDirty(existingPrefab);
        }
    }

    static void HandleRemovedFile(string path)
    {
        // Decide what you want to do here. If the source file is removed
        // do you want to delete the prefab? Maybe ask if you'd like to
        // remove the prefab?
        // NOTE: Because you might get many calls (like you deleted a
        // subfolder full of .test files you might want to get all the
        // filenames and ask all at once ("delete all these prefabs?").
    }

    static void OnPostprocessAllAssets (string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
    {
        foreach (var path in importedAssets)
        {
            if (IsFileWeCareAbout(path))
            {
                HandleAddedOrChangedFile(path);
            }
        }

        foreach (var path in deletedAssets)
        {
            if (IsFileWeCareAbout(path))
            {
                HandleRemovedFile(path);
            }
        }

        for (var ii = 0; ii < movedAssets.Length; ++ii)
        {
            string srcStr = movedFromAssetPaths[ii];
            string dstStr = movedAssets[ii];

            // the source was moved, let's move the corresponding prefab
            // NOTE: We don't handle the case if there already being
            // a prefab of the same name at the destination
            string srcPrefabPath = srcStr + ".prefab";
            string dstPrefabPath = dstStr + ".prefab";

            AssetDatabase.MoveAsset(srcPrefabPath, dstPrefabPath);
        }
    }
}

儲存後,你應該可以將我們上面建立的 Example.test 檔案拖放到 Unity Assets 資料夾中,你應該看到建立了相應的預製件。如果你編輯 Example.test,你將看到預製件中的資料立即更新。如果將預製件拖動到場景層次結構中,你將看到它更新以及 Example.test 更改。如果將 Example.test 移動到另一個資料夾,相應的預製件將隨之移動。如果更改例項上的欄位,則更改 Example.test 檔案,你將看到只有例項上未修改的欄位才會更新。

改進:在上面的示例中,將 Example.test 拖到 Assets 資料夾後,你會看到 Example.testExample.test.prefab。很高興知道讓它的工作更像模型匯入商的工作我們你只是神奇地看到了 Example.test 並且它是一個 AssetBundle 或者其他類似的東西。如果你知道請如何提供該示例