[cb]ScripptableObjectプロローグ
12100 ワード
ユニティManual:http://docs.unity3d.com/Manual/class-ScriptableObject.html
Scripting API:http://docs.unity3d.com/ScriptReference/ScriptableObject.html
Scripptable Objectは、共有するために多くのデータを個別にスクリプトしたインスタンスを格納することができます。このクラスを迷わないでください。Serializable Objectともいいます。ユニティのシリアル化ツールとして理解できます。これはエディタクラスで、Inspectorパネルでデータを編集できます。例えば、百万のデータを記憶しているint[]があると、この配列は4 MBのメモリを占有し、Prefabに置いておくと、あなたが毎回Prefabを実装すると、この配列のコピーが得られます。このPrefabを10個実例化すると、40 MBのメモリを占有します。
Unityのserializesはすべての原生タイプをサポートしています。stings、arrays、lists、UnityのVector 3などもサポートしています。またカスタムクラスもサポートしていますが、シリアルの属性が必要です。
使用状況はScript ableObjectを使用することで、Valuesのコピーを避けるためにメモリ使用量を減らすことが期待されています。しかし、挿入可能なデータセットを定義するためにそれを使用することもできます。この面の例は,RPGゲームのNPCストアをイメージして,カスタマイズしたShopContens Scripptable Objectを作成することができ,各資産は一つのセットが買い物に使えると定義しています。このようなシーンでは、ゲームには3つのエリアがあり、各エリアには異なるレベルのアイテムが提供されます。あなたの店舗シナリオはShopContintsの対象を引用して、どの項目が選択できるかを定義します。
Tips:Scripptable Object参照をチェックすると、Inspectorでこの参照フィールドをダブルクリックできます。ユーザー定義のEditorを作成して、Inspectorで表示されているデータを管理するのに役立ちます。
例えば、一つのゲームの配置表のデータは、Excelなどのツールに企画されて配置されています。ゲームに応用するには、変換が必要です。例えば、類似の変換が必要です。この時、ScripptableObjectを使って、プログラムにアクセスするデータ構造にあらかじめ処理して、Scripptable Objectに保存して、外部ファイルに包装して、ゲームが実行される時に解析や組み立てをする必要がないです。この機能は大量の公衆データに特に役に立つです。
また、Artプロジェクトでステージ情報を作成した後、クライアントはステージの中の要素の位置、大きさなどの情報に基づいてステージを生成します。この中にはステージ情報を格納するプロセスがあります。私たちのやり方はレベルの情報をScripptable Objectに保存して、Editorに現在のレベル情報をProductディレクトリの下に保存してファイルにします。Clientは直接このMapObjectファイルを読みます。
MapSetting.cs
.csharpcode.csharpcode pre&唴13;
{啝13
font-size:small;&啱13;
カラー:black;13;
font-family:consosolas、「Courier New」、courier、monoospace;13;
background-カラー:&菗ffff;13;
//white-space:pre;*/&嗳13;
}&{13;
.csharp code pre{margin:0 em;}&萶13;
.csharrpcode.rem{color=ble 008000;}&菗13;
.csharpcode.kwrd{color=blue 0000 ff}&菗13;
.csharpcode.str{color=blue 006080}&菷13;
.csharpcode.op{color=0000 c 0}&菗13;
.csharpcode.preproc{color=red}13;
.csharpcode.asp{background-カラー}13;
.csharrpcode.{啚80 0000;}13;
.csharpcode.atr{color=blue}13;
.csharpcode.alt&啝13;
{啝13
background-f 4 f 4;13;
width:100%;13;
margin:0 em;13;
}&{13;
.csharpcode.lnum{color=blue 6060}&荆13;
-->
.csharpcode.csharpcode pre&唴13;
{啝13
font-size:small;&啱13;
カラー:black;13;
font-family:consosolas、「Courier New」、courier、monoospace;13;
background-カラー:&菗ffff;13;
//white-space:pre;*/&嗳13;
}&{13;
.csharp code pre{margin:0 em;}&萶13;
.csharrpcode.rem{color=ble 008000;}&菗13;
.csharpcode.kwrd{color=blue 0000 ff}&菗13;
.csharpcode.str{color=blue 006080}&菷13;
.csharpcode.op{color=0000 c 0}&菗13;
.csharpcode.preproc{color=red}13;
.csharpcode.asp{background-カラー}13;
.csharrpcode.{啚80 0000;}13;
.csharpcode.atr{color=blue}13;
.csharpcode.alt&啝13;
{啝13
background-f 4 f 4;13;
width:100%;13;
margin:0 em;13;
}&{13;
.csharpcode.lnum{color=blue 6060}&荆13;
-->
<!--&菗13;
.csharpcode.csharpcode pre&唴13;
{啝13
font-size:small;&啱13;
カラー:black;13;
font-family:consosolas、「Courier New」、courier、monoospace;13;
background-カラー:&菗ffff;13;
//white-space:pre;*/&嗳13;
}&{13;
.csharp code pre{margin:0 em;}&萶13;
.csharrpcode.rem{color=ble 008000;}&菗13;
.csharpcode.kwrd{color=blue 0000 ff}&菗13;
.csharpcode.str{color=blue 006080}&菷13;
.csharpcode.op{color=0000 c 0}&菗13;
.csharpcode.preproc{color=red}13;
.csharpcode.asp{background-カラー}13;
.csharrpcode.{啚80 0000;}13;
.csharpcode.atr{color=blue}13;
.csharpcode.alt&啝13;
{啝13
background-f 4 f 4;13;
width:100%;13;
margin:0 em;13;
}&{13;
.csharpcode.lnum{color=blue 6060}&荆13;
-->
Scripting API:http://docs.unity3d.com/ScriptReference/ScriptableObject.html
Scripptable Objectは、共有するために多くのデータを個別にスクリプトしたインスタンスを格納することができます。このクラスを迷わないでください。Serializable Objectともいいます。ユニティのシリアル化ツールとして理解できます。これはエディタクラスで、Inspectorパネルでデータを編集できます。例えば、百万のデータを記憶しているint[]があると、この配列は4 MBのメモリを占有し、Prefabに置いておくと、あなたが毎回Prefabを実装すると、この配列のコピーが得られます。このPrefabを10個実例化すると、40 MBのメモリを占有します。
Unityのserializesはすべての原生タイプをサポートしています。stings、arrays、lists、UnityのVector 3などもサポートしています。またカスタムクラスもサポートしていますが、シリアルの属性が必要です。
使用状況はScript ableObjectを使用することで、Valuesのコピーを避けるためにメモリ使用量を減らすことが期待されています。しかし、挿入可能なデータセットを定義するためにそれを使用することもできます。この面の例は,RPGゲームのNPCストアをイメージして,カスタマイズしたShopContens Scripptable Objectを作成することができ,各資産は一つのセットが買い物に使えると定義しています。このようなシーンでは、ゲームには3つのエリアがあり、各エリアには異なるレベルのアイテムが提供されます。あなたの店舗シナリオはShopContintsの対象を引用して、どの項目が選択できるかを定義します。
Tips:Scripptable Object参照をチェックすると、Inspectorでこの参照フィールドをダブルクリックできます。ユーザー定義のEditorを作成して、Inspectorで表示されているデータを管理するのに役立ちます。
例えば、一つのゲームの配置表のデータは、Excelなどのツールに企画されて配置されています。ゲームに応用するには、変換が必要です。例えば、類似の変換が必要です。この時、ScripptableObjectを使って、プログラムにアクセスするデータ構造にあらかじめ処理して、Scripptable Objectに保存して、外部ファイルに包装して、ゲームが実行される時に解析や組み立てをする必要がないです。この機能は大量の公衆データに特に役に立つです。
また、Artプロジェクトでステージ情報を作成した後、クライアントはステージの中の要素の位置、大きさなどの情報に基づいてステージを生成します。この中にはステージ情報を格納するプロセスがあります。私たちのやり方はレベルの情報をScripptable Objectに保存して、Editorに現在のレベル情報をProductディレクトリの下に保存してファイルにします。Clientは直接このMapObjectファイルを読みます。
MapSetting.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
// , Client
public class CMapSetting : ScriptableObject
{
public List<string> TexturesDependencies;//
public string MapObjectPath;//MapRoot
public List<CLevelBattleSetting> Battles;//
}
Editor
public void ExportCurrentScene()
{
SceneName = EditorApplication.currentScene.Substring(EditorApplication.currentScene.LastIndexOf('/') + 1);
SceneName = SceneName.Substring(0, SceneName.LastIndexOf('.'));
string exportPath = string.Format("Scene/{0}{1}", SceneName, GameDef.ASSET_BUNDLE_FILE_EXT);
string mapObjectPath = string.Format("Scene/{0}_MapObject{1}", SceneName, GameDef.ASSET_BUNDLE_FILE_EXT);
CSimBattle[] battles = GameObject.FindObjectsOfType<CSimBattle>();// Battle
List<CSimBattle> battleList = new List<CSimBattle>(battles);
battleList.Sort((CSimBattle x, CSimBattle y) =>
{
return x.transform.position.y.CompareTo(y.transform.position.y);
});// Battle Y
CSimActor[] actors = GameObject.FindObjectsOfType<CSimActor>();// Actor
List<CSimActor> actorList = new List<CSimActor>(actors);
int count = 0;// Battle
List<CLevelBattleSetting> LevelBattleSettings = new List<CLevelBattleSetting>();//
for (int b = 0; b < battleList.Count; b++)// Battle
{
CSimBattle simBattle = battleList[b];
CLevelBattleSetting levelBatSetting = new CLevelBattleSetting();// BattleSetting
levelBatSetting.MapActorSettings = new List<CMapActorSetting>();//Battle Actor
levelBatSetting.Num = simBattle.KillNum;
levelBatSetting.Time = simBattle.BattleTime;
levelBatSetting.StoryOnStart = simBattle.StoryOnStart;
levelBatSetting.StoryOnEnd = simBattle.StoryOnEnd;
levelBatSetting.CurPosition = simBattle.transform.position;
//CBase.Log("CSimBattle.CurPosition:{0}", levelBatSetting.CurPosition);
float battlePosY = simBattle.transform.position.y;//Battle Y
for (int a = (actorList.Count - 1); a >= 0; a--)//
{
float actorPosY = actorList[a].transform.position.y;//Actor Y
if (actorPosY <= battlePosY)// Actor Battle
{
CMapActorSetting actorSetting = new CMapActorSetting();
CSimActor simActor = actorList[a];
actorSetting.NpcId = simActor.NPC ;
actorSetting.NpcLevel = simActor.NPC ;
actorSetting.NpcPosition = simActor.transform.position;
actorSetting.IsEnemy = simActor. ;
levelBatSetting.MapActorSettings.Add(actorSetting);//
actorList.Remove(simActor);// Actor
count += 1;
}
}
LevelBattleSettings.Add(levelBatSetting);// battle
CBase.Log("Battle :{0} {1} " ,b, count);// Battle
count = 0;
}
//CBase.Log(" {1} Battle", LevelBattleSettings.Count);
GameObject mapRoot = GameObject.Find("MapRoot");
if (mapRoot != null)
{
List<string> textureList = new List<string>();//
Renderer[] renderers = mapRoot.GetComponentsInChildren<Renderer>();
foreach (Renderer child in renderers)
{
Texture _texture = child.sharedMaterial.mainTexture;
string _path = AssetDatabase.GetAssetPath(_texture);
_path = XBuildTools.GetUniquepath(_path);
if (!textureList.Contains(_path))
{
textureList.Add(_path);
XBuildTools.PushAssetBundle(_texture, string.Format("Textures/{0}{1}", _path, GameDef.ASSET_BUNDLE_FILE_EXT));
CBase.Log("Texture ! =>{0}", _path);
}
}
XBuildTools.PushAssetBundle(mapRoot, mapObjectPath);// mapRoot
Debug.Log(" !" + SceneName);
XBuildTools.PopAssetBundle();
//XBuildTools.PopAllAssetBundle();
CMapSetting mapSetting = ScriptableObject.CreateInstance<CMapSetting>();
mapSetting.TexturesDependencies = textureList;
mapSetting.MapObjectPath = mapObjectPath;
mapSetting.Battles = LevelBattleSettings;
XBuildTools.BuildScriptableObject(mapSetting, exportPath);// MapSetting , 。
}
}
<!--&菗13;.csharpcode.csharpcode pre&唴13;
{啝13
font-size:small;&啱13;
カラー:black;13;
font-family:consosolas、「Courier New」、courier、monoospace;13;
background-カラー:&菗ffff;13;
//white-space:pre;*/&嗳13;
}&{13;
.csharp code pre{margin:0 em;}&萶13;
.csharrpcode.rem{color=ble 008000;}&菗13;
.csharpcode.kwrd{color=blue 0000 ff}&菗13;
.csharpcode.str{color=blue 006080}&菷13;
.csharpcode.op{color=0000 c 0}&菗13;
.csharpcode.preproc{color=red}13;
.csharpcode.asp{background-カラー}13;
.csharrpcode.{啚80 0000;}13;
.csharpcode.atr{color=blue}13;
.csharpcode.alt&啝13;
{啝13
background-f 4 f 4;13;
width:100%;13;
margin:0 em;13;
}&{13;
.csharpcode.lnum{color=blue 6060}&荆13;
-->
:
<!--&菗13;.csharpcode.csharpcode pre&唴13;
{啝13
font-size:small;&啱13;
カラー:black;13;
font-family:consosolas、「Courier New」、courier、monoospace;13;
background-カラー:&菗ffff;13;
//white-space:pre;*/&嗳13;
}&{13;
.csharp code pre{margin:0 em;}&萶13;
.csharrpcode.rem{color=ble 008000;}&菗13;
.csharpcode.kwrd{color=blue 0000 ff}&菗13;
.csharpcode.str{color=blue 006080}&菷13;
.csharpcode.op{color=0000 c 0}&菗13;
.csharpcode.preproc{color=red}13;
.csharpcode.asp{background-カラー}13;
.csharrpcode.{啚80 0000;}13;
.csharpcode.atr{color=blue}13;
.csharpcode.alt&啝13;
{啝13
background-f 4 f 4;13;
width:100%;13;
margin:0 em;13;
}&{13;
.csharpcode.lnum{color=blue 6060}&荆13;
-->
<!--&菗13;
.csharpcode.csharpcode pre&唴13;
{啝13
font-size:small;&啱13;
カラー:black;13;
font-family:consosolas、「Courier New」、courier、monoospace;13;
background-カラー:&菗ffff;13;
//white-space:pre;*/&嗳13;
}&{13;
.csharp code pre{margin:0 em;}&萶13;
.csharrpcode.rem{color=ble 008000;}&菗13;
.csharpcode.kwrd{color=blue 0000 ff}&菗13;
.csharpcode.str{color=blue 006080}&菷13;
.csharpcode.op{color=0000 c 0}&菗13;
.csharpcode.preproc{color=red}13;
.csharpcode.asp{background-カラー}13;
.csharrpcode.{啚80 0000;}13;
.csharpcode.atr{color=blue}13;
.csharpcode.alt&啝13;
{啝13
background-f 4 f 4;13;
width:100%;13;
margin:0 em;13;
}&{13;
.csharpcode.lnum{color=blue 6060}&荆13;
-->