簡體   English   中英

自動填充可編寫腳本的對象

[英]Populate Scriptable Objects Automatically

我的一些可編寫腳本的對象(用於播種我的初始游戲數據)包含大型二維數組(如 10x10),我使用外部 Python 腳本生成其中的數據。 我也使用 Odin 檢查器插件來為我序列化二維數組,並在 Unity 編輯器中為我提供該數組的良好表示。

我只是這樣做:

[TableMatrix()]
public int[,] table = new int[10, 10];

這只是一個 Odin SerializedScriptableObject 類。

問題是,我真的想避免使用 Unity 編輯器手動添加 10x10 元素,而且我希望我的對象具有可變的二維數組大小,一個可能是 (10,10),另一個可能是 (5,5 )。 有沒有辦法以編程方式填充我的可編寫腳本的對象來實現這一目標? (或者,如果有人知道,Odin 檢查器插件是否支持類似的東西?)

謝謝 !

抱歉@Spyros 回復晚了。

我的方法將類似於@D Manokhin 方法,但我將使用多維數組而不是使用鋸齒狀數組(因為您可以構建自定義編輯器腳本來可視化它們,我很確定您可以在編輯器上可視化鋸齒狀數組而無需插件,我從未使用過 Odin 插件)。

所以我定義了一個將存儲名為TileData的結構的TileData

using UnityEngine;
using System.Collections;

[System.Serializable]
public class TileData
{
    /*
     * rows
        [rowData][rowData][rowData]
        [cell]  ->value
                ->type
        [cell]  ->value
                ->type
        [cell]  ->value
                ->type
    */

    [System.Serializable]
    public struct cell
    {
        public float value;
        public string type;
    }


    [System.Serializable]
    public struct rowData
    {
        public cell[] row;
    }

    public rowData[] rows;

}

我使用valuetype作為“示例”,這完全取決於您,如果您願意,您也可以存儲 Vector3!

那么,如何調用和使用這種結構呢? 讓我們來看看:

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

public class Matrix : MonoBehaviour {

    //declare the structure that will store the data
    public TileData tileData = new TileData();    

    //those are public so you could make the matrix with the size you want..
    //..ideally dynamically call it from your python file requisits
    public int horizontalMatrixSize = 10;  
    public int verticalMatrixSize = 10;

    // Use this for initialization
    void Start()
    {        
        //Create the matrix structure and fill it
        tileData.rows = new TileData.rowData[horizontalMatrixSize];
        for (int i = 0; i < tileData.rows.Length; i++)
        {
            tileData.rows[i].row = new TileData.cell[verticalMatrixSize];
            for (int u = 0; u < tileData.rows[i].row.Length; u++)
            {                
                tileData.rows[i].row[u].value = GetValuesFromPythonFileOrWhatever();
                tileData.rows[i].row[u].type = GetValuesFromPythonFileOrWhatever();
            }
        }
    }

}

但是如果你真的喜歡鋸齒狀的結構,你仍然可以像這樣使用它(但請記住它不會在編輯器中顯示):

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

public class Matrix : MonoBehaviour {
    //declare the structure that will store the data
    [SerializeField] private float[,] grayscaleBidimensional = null;

    //those are public so you could make the matrix with the size you want..
    //..ideally dynamically call it from your python file requisits
    public int horizontalMatrixSize = 10;  
    public int verticalMatrixSize = 10;

    // Use this for initialization
    void Start()
    {        
        //Create the matrix structure and fill it
        tileData.rows = new TileData.rowData[horizontalMatrixSize];

        grayscaleBidimensional = new float[horizontalMatrixSize, verticalMatrixSize];
        for (int x = 0; x < horizontalMatrixSize; x++)
        {
            for (int y = 0; y < verticalMatrixSize; y++)
            {
                grayscaleBidimensional[x, y] = GetValuesFromPythonFileOrWhatever();
            }
        }
    }

}

請記住,您可以根據需要創建對象的值,因此它們不需要具有靜態大小!

要更改單個值,您可以執行table[x,y] = (your value) 假設您想用第一個填充每個值,您可以這樣做:

for (int y = 0; y < 10; y++)
{
    for (int x = 0; x < 10; x++)
    {
        table[x, y] = 1;
    }
}

希望能回答您的問題,如果代碼有誤,請見諒 - 我目前無法訪問 Unity,因此您可能需要擺弄它。

您應該將數組序列化為 python 中的文件,然后使用腳本導入器將它們導入 Unity,在 ScriptableObject 中構造數組,並填寫它們的值。

https://docs.unity3d.com/Manual/ScriptedImporters.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM