Jump to content
Unity Insider Forum

Leader_Mike_2011

Members
  • Gesamte Inhalte

    111
  • Benutzer seit

  • Letzter Besuch

  • Tagessiege

    1

Alle erstellten Inhalte von Leader_Mike_2011

  1. Abend Also ich möchte in meinem Spiel zwischendurch immer wieder kleine Zwischensequenzen (in Form von Filmen, die mit Cinema 4D erstellt wurden) abspielen. Mein Problem ist, dass die Filme immer eine extrem schlechte Qualität haben (besitze Unity 3D Pro). Danke für eure Antworten
  2. Hey, ich hab mal wieder eine Frage Ich habe ein paar Autos, bei denen das Autoscript an einer Ampel deaktivert werden soll. Zunächst suche ich nach Autos mit folgendem Tag: private GameObject[] Cars; Cars = GameObject.FindGameObjectsWithTag("Car_1"); Jetzt möchte ich das bei allen Gameobjekten ein bestimmtes Script deaktivert wird. Cars.GetComponents<drivingscript>().enabled = false; <= Fehler liegt natürlich hier, da ich auf meherere GameObjekte zurückgreife Ich weiß das man das mit einer for Schleife realisieren kann, aber ich bekomme das mit C# nicht hin... Schonmal vielen Dank im vorraus für eure Hilfe
  3. Guten Morgen^^ Kann mir vielleicht jemand mit meinem Script zum Speichern und Laden von Objekten weiterhelfen? Mithilfe des Scripts will ich Objekte die ich per Tastatureingabe in-game auf dem terrain setzten kann speichern und laden (Objekte mit Tag "SaveGame"). Ich bekomme zwar keine Errors und die Objekte werden in der XML Datei korrekt abgespeichert, allerdings werden diese dann nicht mehr ins Spiel geladen. Tip: Fehler sollte in OnGUI() liegen wo ich die Objekte Instantiere^^ Hier ist das Script: using UnityEngine; using System.Collections; using System.Xml; using System.Xml.Serialization; using System.IO; using System.Text; public GameObject meinPrefab; //Objekt-Prefab, damit beim Laden die Objekte instanziiert werden können public class _GameSaveLoad: MonoBehaviour { Rect _Save, _Load, _SaveMSG, _LoadMSG; bool _ShouldSave, _ShouldLoad,_SwitchSave,_SwitchLoad; string _FileLocation,_FileName; public GameObject _Player; UserData myData; string _data; public GameObject[] myObjects; float VPositionX; float VPositionY; float VPositionZ; int VarrayCount; // When the EGO is instansiated the Start will trigger // so we setup our initial values for our local members void Start () { // We setup our rectangles for our messages _Save=new Rect(10,80,100,20); _Load=new Rect(10,100,100,20); _SaveMSG=new Rect(10,120,400,40); _LoadMSG=new Rect(10,140,400,40); // Where we want to save and load to and from _FileLocation=Application.dataPath; _FileName="Unity3DWorldsSave.xml"; // we need soemthing to store the information into myData=new UserData(); } void OnGUI() { //*************************************************** // Loading The Player... // ************************************************** if (GUI.Button(_Load,"Load")) { GUI.Label(_LoadMSG,"Loading from: "+_FileLocation); // Load our UserData into myData LoadXML(); if(_data.ToString() != "") { // notice how I use a reference to type (UserData) here, you need this // so that the returned object is converted into the correct type myData = (UserData)DeserializeObject(_data); // Anzahl der Objekte laden VarrayCount = myData._iUser.arrayCount; //myObjects = GameObject.FindGameObjectsWithTag("SaveTags"); for (var i = 0; i < VarrayCount; i++) //alle Elemente der Array durchgehen { //Daten laden VPositionX = myData._iUser.xArray[i]; VPositionY = myData._iUser.yArray[i]; VPositionZ = myData._iUser.zArray[i]; //Objekt erstellen Instantiate (meinPrefab, new Vector3(VPositionX, VPositionY, VPositionZ), Quaternion.identity); } // just a way to show that we loaded in ok //Debug.Log(myData._iUser.name); } } //*************************************************** // Saving The Player... // ************************************************** if (GUI.Button(_Save,"Save")) { myObjects = GameObject.FindGameObjectsWithTag("SaveTags"); myData._iUser.xArray = new float[myObjects.Length]; //Array mit so vielen Elementen wie zu speichernden Objekten myData._iUser.yArray = new float[myObjects.Length]; myData._iUser.zArray = new float[myObjects.Length]; GUI.Label(_SaveMSG,"Saving to: "+_FileLocation); for (var i = 0; i < myObjects.Length; i++) //alle Elemente der Array durchgehen { myData._iUser.xArray[i] = myObjects[i].transform.position.x; //x-Position des aktuellen Elements an die richtige Stelle in die Array der x-Werte schreiben myData._iUser.yArray[i] = myObjects[i].transform.position.y; myData._iUser.zArray[i] = myObjects[i].transform.position.z; } // Create the XML _data = SerializeObject(myData); // This is the final resulting XML from the serialization process CreateXML(); Debug.Log(_data); } } /* The following metods came from the referenced URL */ string UTF8ByteArrayToString(byte[] characters) { UTF8Encoding encoding = new UTF8Encoding(); string constructedString = encoding.GetString(characters); return (constructedString); } byte[] StringToUTF8ByteArray(string pXmlString) { UTF8Encoding encoding = new UTF8Encoding(); byte[] byteArray = encoding.GetBytes(pXmlString); return byteArray; } // serialize the UserData object of myData string SerializeObject(object pObject) { string XmlizedString = null; MemoryStream memoryStream = new MemoryStream(); XmlSerializer xs = new XmlSerializer(typeof(UserData)); XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8); xs.Serialize(xmlTextWriter, pObject); memoryStream = (MemoryStream)xmlTextWriter.BaseStream; XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray()); return XmlizedString; } // Deserialize it back into its original form object DeserializeObject(string pXmlizedString) { XmlSerializer xs = new XmlSerializer(typeof(UserData)); MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString)); XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8); return xs.Deserialize(memoryStream); } // Finally the save and load methods for the file itself void CreateXML() { StreamWriter writer; FileInfo t = new FileInfo(_FileLocation+"\\"+ _FileName); if(!t.Exists) { writer = t.CreateText(); } else { t.Delete(); writer = t.CreateText(); } writer.Write(_data); writer.Close(); Debug.Log("File written."); } void LoadXML() { StreamReader r = File.OpenText(_FileLocation+"\\"+ _FileName); string _info = r.ReadToEnd(); r.Close(); _data=_info; Debug.Log("File Read"); } } // UserData is our custom class that holds our defined objects we want to store in XML format public class UserData { // Define a default instance of the structure public DemoData _iUser; // Default constructor doesn't really do anything at the moment public UserData() { } // Store in the XML file, defined here public struct DemoData { public float[] xArray; //Array für alle X-Positionen public float[] yArray; //Array für alle Y-Positionen public float[] zArray; //Array für alle Z-Positionen public int arrayCount; //Anzahl der zu speichernden/zu ladenden Objekte } } ich hoffe ihr könnt den Fehler finden ich bin echt am verzweifeln PS: Schonmal Danke im Vorraus
×
×
  • Neu erstellen...