ジョエル・フェルナンデス:
私はランダムにスクリプト可能オブジェクトから項目を選択し、コンソールにランダムに選択した項目を印刷したいです。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "Country", menuName = "Country/Country", order = 0)]
public class country : ScriptableObject
{
[System.Serializable]
public class Item
{
public string Name;
public string Currency;
public string Capital;
public string[] City;
}
public Item[] m_Items;
}
どのように私は、コンソールに次の値を印刷するに行くのですか?
public Item PickRandomly()
{
int index = Random.Range(0, m_Items.Length);
return m_Items[index];
}
クイントレオ:
あなたはそのようなあなたのクラスのToStringメソッド()関数をオーバーライドすることができます。
[System.Serializable]
public class Item
{
public string Name;
public string Currency;
public string Capital;
public string[] City;
public override string ToString()
{
string toPrint = "Name: " + this.Name + " Currency: " + this.Currency + " Capital:" + this.Capital;
if(City != null)
{
toPrint += " Cities: ";
for(int i =0; i < City.Length; ++i)
{
toPrint += City[i];
if(i < City.Length -1)
{
toPrint += ",";
}
else
{
toPrint += ".";
}
}
}
return toPrint;
}
}
その後、あなたは、単にDEBUG.LOG(PickRandomlyを())を呼び出すことができます。出力は次のようなものでなければなりません:「名前:カナダ通貨:CAD資本金:オタワ都市:トロント、モントリオール、バンクーバー」。あなたが好む出力とにかくを微調整することができます。