代码之家  ›  专栏  ›  技术社区  ›  Spyros

自动填充可编写脚本的对象

  •  1
  • Spyros  · 技术社区  · 6 年前

    我的一些可编写脚本的对象(用于植入我的初始游戏数据)包含大型二维数组(如10x10),我使用外部Python脚本生成这些数组的数据。我确实也使用了OdinInspector插件,为我序列化2d数组,并在Unity编辑器中为我提供该数组的良好表示。

    我只是这样做:

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

    这只是一个Odin SerializedScriptableObject类。

    谢谢

    3 回复  |  直到 6 年前
        1
  •  1
  •   Lotan    6 年前

    很抱歉@Spyros回复太晚。

    我的方法将类似于@D Manokhin方法,但我将使用多维数组(因为你可以构建一个自定义编辑器脚本来可视化它们,我很确定你可以在没有插件的编辑器上可视化锯齿数组,我从未使用过Odin插件)。

    所以我需要一个类来存储名为 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;
    
    }
    

    我曾经 value type

    那么,如何调用和使用这种结构呢?让我们看看:

    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();
                }
            }
        }
    
    }
    

    请记住,您可以根据需要生成对象的值,因此它们不需要具有静态大小!

        2
  •  0
  •   Julxzs Danny Herbert    6 年前

    table[x,y] = (your value) . 假设要用数字1填充每个值,可以执行以下操作:

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

    希望这能回答你的问题,如果代码错了,我很抱歉——我现在无法访问Unity,所以你可能需要摆弄它。

        3
  •  0
  •   Leo Bartkus    6 年前

    您应该将数组序列化为python中的文件,然后使用脚本导入器将它们导入Unity,在ScriptableObject中构造数组,并填充它们的值。

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