using UnityEngine; using System.Collections; public class SaveLoad : MonoBehaviour { public Transform CurrentPlayerPosition; void Update () { if(Input.GetKeyDown(KeyCode.R)) savePosition(); if(Input.GetKeyDown(KeyCode.L)) if (PlayerPrefs.HasKey("PosX")) // , loadPosition(); if(Input.GetKeyDown(KeyCode.D)) PlayerPrefs.DeleteAll(); // } public void savePosition(){ Transform CurrentPlayerPosition = this.gameObject.transform; PlayerPrefs.SetFloat("PosX", CurrentPlayerPosition.position.x); // .. PlayerPrefs.SetFloat("PosY", CurrentPlayerPosition.position.y); // , PlayerPrefs.SetFloat("PosZ", CurrentPlayerPosition.position.z); // float PlayerPrefs.SetFloat("AngX", CurrentPlayerPosition.eulerAngles.x); PlayerPrefs.SetFloat("AngY", CurrentPlayerPosition.eulerAngles.y); PlayerPrefs.SetString("level", Application.loadedLevelName); // / PlayerPrefs.SetInt("level_id", Application.loadedLevel); // } public void loadPosition(){ Transform CurrentPlayerPosition = this.gameObject.transform; Vector3 PlayerPosition = new Vector3(PlayerPrefs.GetFloat("PosX"), PlayerPrefs.GetFloat("PosY"), PlayerPrefs.GetFloat("PosZ")); Vector3 PlayerDirection = new Vector3(PlayerPrefs.GetFloat("AngX"), // PlayerPrefs.GetFloat("AngY"), 0); // CurrentPlayerPosition.position = PlayerPosition; // CurrentPlayerPosition.eulerAngles = PlayerDirection; } }
PlayerPrefs
QualitySettings.SetQualityLevel
. using UnityEngine; using System.Collections.Generic; using System.Xml.Serialization; using System; [XmlRoot("RoomState")] [XmlInclude(typeof(PositData))] public class RoomState { // , [XmlArray("Furniture")] [XmlArrayItem("FurnitureObject")] public List<PositData> furniture = new List<PositData>(); // public RoomState() { } // public void AddItem(PositData item) { // - furniture.Add(item); // } public void Update(){ // , - foreach (PositData felt in furniture) // felt.Update(); } }
[XmlType("PositionData")] [XmlInclude(typeof(Lamp))] public class PositData { protected GameObject _inst; // public GameObject inst { set { _inst = value; } } [XmlElement("Type")] public string Name { get; set; } // Resourses [XmlElement("Position")] public Vector3 position {get; set; } public PositData() { } public PositData(string name, Vector3 position) { this.Name = name; this.position = position; } public virtual void Estate(){ } // "" public virtual void Update(){ // position = _inst.transform.position; // } } [XmlType("Lamp")] public class Lamp : PositData // , , / { [XmlAttribute("Light")] public bool lightOn { get; set; } public Lamp() { } public Lamp(string name, Vector3 position, bool lightOn): base(name, position) { this.lightOn = lightOn; } public override void Estate(){ if (!lightOn) ((Light)(_inst.GetComponentInChildren(typeof(Light)))).enabled = false; } // , Light public override void Update(){ base.Update(); lightOn = ((Light)_inst.GetComponentInChildren(typeof(Light))).enabled; } // lightOn = Light }
using System.Xml.Serialization; using System; using System.IO; public class Serializator { static public void SaveXml(RoomState state, string datapath){ Type[] extraTypes= { typeof(PositData), typeof(Lamp)}; XmlSerializer serializer = new XmlSerializer(typeof(RoomState), extraTypes); FileStream fs = new FileStream(datapath, FileMode.Create); serializer.Serialize(fs, state); fs.Close(); } static public RoomState DeXml(string datapath){ Type[] extraTypes= { typeof(PositData), typeof(Lamp)}; XmlSerializer serializer = new XmlSerializer(typeof(RoomState), extraTypes); FileStream fs = new FileStream(datapath, FileMode.Open); RoomState state = (RoomState)serializer.Deserialize(fs); fs.Close(); return state; } }
public class RoomGen : MonoBehaviour { private RoomState state; // private string datapath; // void Start () { datapath = Application.dataPath + "/Saves/SavedData" + Application.loadedLevel + ".xml"; if (File.Exists(datapath)) // state = Serializator.DeXml(datapath); // state else setDefault(); // Generate(); // state } void setDefault(){ state = new RoomState(); // chair, table, lamp - Resourses state.AddItem(new PositData("chair", new Vector3(15f, 1f, -4f))); state.AddItem(new PositData("chair", new Vector3(10f, 1f, 0f))); state.AddItem(new PositData("table", new Vector3(5f, 1f, 4f))); state.AddItem(new Lamp("lamp", new Vector3(5f, 4f, 4f), true)); } void Generate(){ foreach (PositData felt in state.furniture){ // felt.inst = Instantiate(Resources.Load(felt.Name), felt.position, Quaternion.identity) as GameObject; // felt.Estate(); // } } void Dump() { state.Update(); // state Serializator.SaveXml(state, datapath); // } }
using UnityEngine; using System.Collections; public string nextRoom; public class Door : MonoBehaviour { void OnTriggerEnter(Collider hit) { if (hit.gameObject.tag == "Player") { SendMessageUpwards("Dump"); Application.LoadLevel(nextRoom); } } }
OnGUI()
is a MonoBehaviour function for drawing GUIs and handling related events. Something like Update (), but specifically for the GUI and can be called more often than each frame.GUI.Button
static bool Button(Rect position, String text); static bool Button(Rect position, Textureimage );
GUI.BeginGroup
, GUI.EndGroup
static void BeginGroup (Rect position); static void EndGroup ();
public class MenuScript : MonoBehaviour { public Texture2D backgroundTexture; // public const int mainMenuWidth = 200; // private int menutype = 0; // - private bool menuMode = true; // private bool gameMode = false; // - // false Load / New Game private void Awake(){ DontDestroyOnLoad(this); // } void Update () { if (Input.GetKeyDown(KeyCode.Escape)){ // warning! : Input.GetKey() if(gameMode) if (menutype == 0 || !menuMode){ // switchGameActivity(); // / // ( ) menuMode = !menuMode; } menutype = 0; // } // } private void OnGUI(){ if (menuMode){ if(backgroundTexture != null) // , GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), backgroundTexture); switch (menutype){ case 0: drawMainMenu(); break; case 1: case 2: drawSaveLoadMenu(); break; } } } private void drawMainMenu(){ GUI.BeginGroup (new Rect (Screen.width / 2 - mainMenuWidth/2, Screen.height / 2 - 180, mainMenuWidth, 240)); if (gameMode) if(GUI.Button(new Rect(0,0, mainMenuWidth,30) , "Resume")){ menuMode = false; switchMouseLook(); } if(GUI.Button(new Rect(0, 40, mainMenuWidth, 30) , "New Game")){ menuMode = false; gameMode = true; Application.LoadLevel("first_scene"); } if (gameMode) if(GUI.Button(new Rect(0, 2*40, mainMenuWidth, 30) , "Save")) menutype = 1; if(GUI.Button(new Rect(0, ((gameMode)? 3 : 2)*40, mainMenuWidth, 30) , "Load")){ menutype = 2; // "2 + gameMode"? C#, nuff said. if(GUI.Button(new Rect(0, ((gameMode)? 4 : 3)*40, mainMenuWidth, 30) , "Options")){} if(GUI.Button(new Rect(0, ((gameMode)? 5 : 4)*40, mainMenuWidth, 30) , "Quit Game")){ Application.Quit(); } GUI.EndGroup(); } void switchGameActivity(){ // c, MouseLook, Camera mk = Camera.main; // . MouseLook ml = mk.GetComponent<MouseLook>(); // if (ml != null) ml.enabled = !ml.enabled; // } }
GUI.SelectionGrid
- draws a grid of buttons, but in fact it is a single variant select. One option is always selected, returns the number of the selected one. static int SelectionGrid (Rect position , int defaultSelected, Texture[] images, int elsInRow); static int SelectionGrid (Rect position , int defaultSelected, string[] texts, int elsInRow);
public int selGridInt = 0; // public string[] selStrings = new string[] {"Grid 1", "Grid 2", "Grid 3", "Grid 4"}; void OnGUI() { // selGridInt = GUI.SelectionGrid(new Rect(25, 25, 100, 30), selGridInt, selStrings, 2); // selGridInt , } // ,
public const int slotsAmount = 10; // / // . , private Texture2D[] saveTexture = new Texture2D[slotsAmount]; // , //, , null private void drawSaveLoadMenu(){ if(GUI.Button(new Rect(Screen.width / 2 - 100, Screen.height * 2/3 + 50, 200, 30) , "Back")) menutype = 0; int slot = GUI.SelectionGrid( new Rect( // 5x2 Screen.width / 2 - Screen.height * 5/9, // Screen.height/3, // 4:3 Screen.height * 10/9, Screen.height/3 ), -1, // saveTexture, // null, 5); // if (slot >= 0) // if (menutype == 1) savegame(slot); // - else if (menutype == 2 && saveTexture[slot] != null) loadgame (slot); } // - . , //
public const int slotsAmount = 10; public const int hN = 5; public const int margin = 20; static private int vN = (int)Mathf.Ceil((float)slotsAmount/hN); private Texture2D[] saveTexture = new Texture2D[slotsAmount]; private int slotSize = ((Screen.width*vN)/(Screen.height*hN) >= 1) ? Screen.height/(vN + 2) : Screen.width/(hN + 2); private void drawSaveLoadMenu(){ GUI.BeginGroup (new Rect ( Screen.width / 2 - (slotSize*hN - margin) / 2, Screen.height / 2 - (slotSize*vN - margin) / 2, slotSize*hN - margin, slotSize*vN + 40)); for (int j = 0; j < vN; j++) for (int i = 0, curr = j*hN; (curr = j*hN + i) < slotsAmount; i++){ if (menutype == 2 && saveTexture[curr] == null) GUI.Box(new Rect(slotSize*i, slotSize*j, slotSize - margin, slotSize - margin), ""); else if(GUI.Button(new Rect(slotSize*i, slotSize*j, slotSize - margin, slotSize - margin), saveTexture[curr])){ if (menutype == 1) savestuff(curr); else if (menutype == 2) loadstuff (curr); } } if(GUI.Button(new Rect(slotSize*hN/2 - 100, slotSize*vN , 200, 30) , "Back")) menutype = 0; GUI.EndGroup(); }
private Texture2D[] saveTexture = new Texture2D[slotsAmount];
int gS = PlayerPrefs.GetInt("gamesSaved"); for (int i = 0; i < slotsAmount && gS > 0; i++, gS/=2) if (gS%2 != 0){ saveTexture[i] = new Texture2D(Screen.width/4, Screen.height/4); // , saveTexture[i].LoadImage(System.IO.File.ReadAllBytes(Application.dataPath + "/tb/Slot" + i + ".png")); } // , , , .
Application.CaptureScreenshot
, but there are two tricks at once. First of all, they are saved in full size, and since in the final analysis we need only thumbnails, it is more logical to resize right away. Secondly, we hold the array of textures, will you have to read it again from the disk? Not very cool. IEnumerator readScreen(int i){ yield return new WaitForEndOfFrame(); // , :) int adjustedWidth = Screen.height * 4/3; // , Texture2D tex1 = new Texture2D(adjustedWidth, Screen.height); // tex1.ReadPixels(new Rect((Screen.width - adjustedWidth)/2, 0, adjustedWidth, Screen.height), 0, 0, true); tex1.Apply(); // Texture2D tex = new Texture2D(Screen.height/3, Screen.height/4, TextureFormat.RGB24, true); tex.SetPixels(tex1.GetPixels(2)); // Destroy(tex1); tex.Apply(); saveTexture[i] = tex; // FileStream fs = System.IO.File.Open(Application.dataPath + "/tb/Slot" + i + ".png", FileMode.Create); BinaryWriter binary = new BinaryWriter(fs); binary.Write(tex.EncodeToPNG()); // fs.Close(); }
GameObject.Find
GameObject.FindWithTag
β / β . , β , , , / , . void savegame(int i) { PlayerPrefs.SetInt("slot" + i + "_Lvl", Application.loadedLevel); // // ! Interface i = Camera.main.GetComponent<Interface>(); if (i != null) i.save(i); // PlayerPrefs menuMode = false; // switchGameActivity(); // // . . // , . // PlayerPrefs.SetInt("gamesSaved", PlayerPrefs.GetInt("gamesSaved") | (1 << i)); StartCoroutine(readScreen(i)); // }
void loadgame(int i) { if (gameMode) // Load , switchGameActivity(); PlayerPrefs.SetInt("Load", i); // , Application.LoadLevel(PlayerPrefs.GetInt("slot" + i + "_Lvl")); // menuMode = false; gameMode = true; // , }
public class Interface : MonoBehaviour { private Transform CurrentPlayerPosition; public virtual void Start () { int load = PlayerPrefs.GetInt("Load"); // , if (load >= 0){ // - load(load); PlayerPrefs.SetInt("Load", -1); // Load } } public virtual void save(int i) { // CurrentPlayerPosition = this.gameObject.transform.parent.transform; PlayerPrefs.SetFloat("slot" + i + "_PosX", CurrentPlayerPosition.position.x); PlayerPrefs.SetFloat("slot" + i + "_PosY", CurrentPlayerPosition.position.y); PlayerPrefs.SetFloat("slot" + i + "_PosZ", CurrentPlayerPosition.position.z); } public virtual void load(int i) { CurrentPlayerPosition = this.gameObject.transform.parent.transform; // Vector3 PlayerPosition = new Vector3(PlayerPrefs.GetFloat("slot" + i + "_PosX"), PlayerPrefs.GetFloat("slot" + i + "_PosY"), PlayerPrefs.GetFloat("slot" + i + "_PosZ")); CurrentPlayerPosition.position = PlayerPosition; // , } // }
public class InterfaceWAng : Interface { public override void Start () { base.Start(); } public override void save(int i) { base.save(i); PlayerPrefs.SetFloat("slot" + i + "_AngX", CurrentPlayerPosition.eulerAngles.x); PlayerPrefs.SetFloat("slot" + i + "_AngY", CurrentPlayerPosition.eulerAngles.y); } public override void load(int i) { base.load(i); Vector3 PlayerDirection = new Vector3(PlayerPrefs.GetFloat("slot" + i + "_AngX"), PlayerPrefs.GetFloat("slot" + i + "_AngY"), 0); CurrentPlayerPosition.eulerAngles = PlayerDirection; } }
Source: https://habr.com/ru/post/163071/
All Articles