2016-09-06 2 views
2

これは厳密にUnity3Dであり、私は100の配列を持っていますTransformUnity3dのテキストファイルにいくつかのTransformを書き込んで読み込む簡単な方法はありますか?

私はそれらをPCのデスクトップ上のファイルに書き、後でそれらを読みたいと思います。

は... ...

// write to desktop... 
string s = ""; 
for (int i=0; i< sendMe.Length; ++i) 
    { 
    Transform tt = sendMe[i].transform; 
    s = s +tt.position.x +"," +tt.position.x +"," ...etc... "\n"; 
    } 

string d = System.Environment.GetFolderPath(
    System.Environment.SpecialFolder.DesktopDirectory); 
string path = System.IO.Path.Combine(d, "happyData.txt"); 

System.IO.File.Delete(path); 
System.IO.File.WriteAllText(path,s); 
return; 

を検討してからちょうど手動でテキスト形式を解析し、同様にそれを読んで。何かのように...

public ReadTrackFromDesktopFile() 
    { 
    GetSplineGetReady("development"); 
    string tt = File.ReadAllText(.. path as above); 
    List<string> ts = new List<string>(
    tt.Split(new string[] { "\r","\n" }, 
    StringSplitOptions.RemoveEmptyEntries)); 
    foreach (string t in ts) 
    { 
    string[] parts = t.Split(','); 
    Vector3 pos = new Vector3(parts[0],parts[1],parts[2]); 
    Quaternion rot = new Quaternion(parts[3],parts[4],parts[5],parts[6]); 
    GetSplineOneNode(pos, rot); 
    } 
    GetSplineFinalise(); 
    } 

しかし、これはナイーブだと思われます。これを行うための「単純な統一方法」は何ですか?

+0

Jsonとplayerprefsは許可されていますか? – Programmer

+0

@JoeBlowあなたがこれを見たことがありますか:https://gitgud.io/TheSniperFan/unityserializer-ng –

+0

こんにちはプロ、Json確かめてください! playerprefs - 私はちょうどデスクトップのテキストファイルに書き込むと思います(上のコード行のみです)。それから、それらを入れ替えたり、変更したりすることができます。 – Fattie

答えて

1

JsonPlayerprefsを使用して変換を保存できます。これはそれを行うことができTransformSaver拡張クラスである:jsonとその逆に配列を変換するための

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

public static class TransformSaver 
{ 

    [System.Serializable] 
    public class TransformInfo 
    { 
     public Vector3 pos; 
     public Quaternion rot; 
     public Vector3 scale; 
    } 

    //Save Transform 
    public static void SaveTransform(this Transform trans, Transform[] tranformToSave) 
    { 
     TransformInfo[] trnfrm = new TransformInfo[tranformToSave.Length]; 
     for (int i = 0; i < trnfrm.Length; i++) 
     { 
      trnfrm[i] = new TransformInfo(); 
      trnfrm[i].pos = tranformToSave[i].position; 
      trnfrm[i].rot = tranformToSave[i].rotation; 
      trnfrm[i].scale = tranformToSave[i].localScale; 
     } 

     string jsonTransform = JsonHelper.ToJson(trnfrm, true); 
     PlayerPrefs.SetString("transform", jsonTransform); 
    } 

    //Load Transform 
    public static Transform[] LoadTransform(this Transform trans) 
    { 
     string jsonTransform = PlayerPrefs.GetString("transform"); 
     if (jsonTransform == null) 
     { 
      return null; 
     } 
     //Debug.Log("Loaded: " + jsonTransform); 

     TransformInfo[] savedTransforms = JsonHelper.FromJson<TransformInfo>(jsonTransform); 
     GameObject[] gameObjects = new GameObject[savedTransforms.Length]; 
     Transform[] loadedTransforms = new Transform[savedTransforms.Length]; 

     for (int i = 0; i < gameObjects.Length; i++) 
     { 
      gameObjects[i] = new GameObject("_"); 
      gameObjects[i].hideFlags = HideFlags.HideAndDontSave; 
      loadedTransforms[i] = gameObjects[i].transform; 
      loadedTransforms[i].position = savedTransforms[i].pos; 
      loadedTransforms[i].rotation = savedTransforms[i].rot; 
      loadedTransforms[i].localScale = savedTransforms[i].scale; 
     } 
     return loadedTransforms; 
    } 

    public static void CopyTransform(this Transform trans, Transform source, Transform target, bool createNewInstance = false) 
    { 
     if (source == null) 
     { 
      return; 
     } 

     if (target == null || createNewInstance) 
     { 
      GameObject obj = new GameObject("_"); 
      obj.hideFlags = HideFlags.HideAndDontSave; 
      target = obj.transform; 
     } 

     target.position = source.position; 
     target.rotation = source.rotation; 
     target.localScale = source.localScale; 
    } 

    public static void CopyTransform(this Transform trans, Transform[] source, Transform[] target, bool createNewInstance = false) 
    { 
     if (source == null || source.Length <= 0) 
     { 
      return; 
     } 

     for (int i = 0; i < target.Length; i++) 
     { 
      CopyTransform(null, source[i], target[i], createNewInstance); 
      if (i >= target.Length - 1) 
      { 
       break; 
      } 
     } 
    } 
} 

JsonHelperスクリプト:

using UnityEngine; 
using System.Collections; 
using System; 

public class JsonHelper 
{ 

    public static T[] FromJson<T>(string json) 
    { 
     Wrapper<T> wrapper = UnityEngine.JsonUtility.FromJson<Wrapper<T>>(json); 
     return wrapper.Items; 
    } 

    public static string ToJson<T>(T[] array, bool prettyPrint) 
    { 
     Wrapper<T> wrapper = new Wrapper<T>(); 
     wrapper.Items = array; 
     return UnityEngine.JsonUtility.ToJson(wrapper, prettyPrint); 
    } 

    [Serializable] 
    private class Wrapper<T> 
    { 
     public T[] Items; 
    } 
} 

使用

using UnityEngine; 
using System.Collections; 

public class TransformTest : MonoBehaviour 
{ 

    public Transform[] objectToSave; 

    // Use this for initialization 
    void Start() 
    { 
     //Save Transform 
     transform.SaveTransform(objectToSave); 

     //Load Transform 
     Transform[] loadedTransform = transform.LoadTransform(); 
     transform.CopyTransform(loadedTransform, objectToSave); 
    } 
} 

それは完璧ではありません改善することができる。改善には、​​関数を配列を返す代わりに、パラメータとしてtransform配列にすることが含まれます。

+0

ファイルの代わりに、PlayerPrefs.SetString( "transform "、jsonTransform); ** ** File.WriteAllText(" yourpath/transform.txt "、jsonTransform); ** **置き換え**' string jsonTransform = PlayerPrefs.GetString( "transform"); ** ** 'jsonTransform = File.ReadAllText("あなたのパス/トランスフォーム.txt ");'。 – Programmer

+0

もう一度ありがとう! – Fattie

1

Binary Serializationを使用できます。

次作成し、構造体:

[Serializable] 
public struct SerializebleVector 
{ 
    public float x, y, z, w; 

    public SerializebleVector(float x, float y, float z, float w = 0f) 
    { 
     this.x = x; 
     this.y = y; 
     this.z = z; 
     this.w = w; 
    } 

    public static explicit operator SerializebleVector(Quaternion a) 
    { 
     return new SerializebleVector(a.x, a.y, a.z, a.w); 
    } 

    public static implicit operator SerializebleVector(Vector3 a) 
    { 
     return new SerializebleVector(a.x, a.y, a.z); 
    } 

} 

[Serializable] 
public struct SerializebleTransform 
{ 
    SerializebleVector position; 
    SerializebleVector rotation; 
    SerializebleVector scale; 

    public SerializebleTransform(Transform tr) 
    { 
     position = tr.position; 
     rotation = (SerializebleVector) tr.rotation; 
     scale = tr.lossyScale; 
    } 


    public static implicit operator SerializebleTransform(Transform tr) 
    { 
     return new SerializebleTransform(tr); 
    } 
} 

保存方法:

public static void Save(Transform[] ts) 
{ 
    var data = new List<SerializebleTransform>(); 

    foreach (var t in ts) 
    { 
     data.Add(t); 
    } 

    BinaryFormatter bf = new BinaryFormatter(); 
    using (FileStream file = File.Create (Application.persistentDataPath + "/savedTransforms.dat")) 
    { 
     bf.Serialize(file, data); 
    } 
} 

ロード方式:

public static void Load(out List<SerializebleTransform> ts) 
{ 
    if(File.Exists(Application.persistentDataPath + "/savedTransforms.dat")) 
    { 
     BinaryFormatter bf = new BinaryFormatter(); 
     using (FileStream file = File.Open(Application.persistentDataPath + "/savedTransforms.dat", FileMode.Open)) 
     { 
      var data = (List<SerializebleTransform>)bf.Deserialize(file); 
      return data; 
     } 
    } 
    else 
     ts = null; 
} 

PS:私の知る限りはあなたを知っているようTransformを作成することはできませんGameObjectがないため、読み取りデータfr om SerializebleTransformTransform種類のを手動で入力します。

関連する問題