Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// This container is used in OnPhotonSerializeView() to either provide incoming data of a PhotonView or for you to provide it.
- /// </summary>
- /// <remarks>
- /// The IsWriting property will be true if this client is the "owner" of the PhotonView (and thus the GameObject).
- /// Add data to the stream and it's sent via the server to the other players in a room.
- /// On the receiving side, IsWriting is false and the data should be read.
- ///
- /// Send as few data as possible to keep connection quality up. An empty PhotonStream will not be sent.
- ///
- /// Use either Serialize() for reading and writing or SendNext() and ReceiveNext(). The latter two are just explicit read and
- /// write methods but do about the same work as Serialize(). It's a matter of preference which methods you use.
- /// </remarks>
- /// \ingroup publicApi
- public class PhotonStream
- {
- private List<object> writeData;
- private object[] readData;
- private int currentItem; //Используется для отслеживания следующего элемента для получения.
- /// <summary>Если true, этот клиент должен добавить данные в поток, чтобы отправить его.</summary>
- public bool IsWriting { get; private set; }
- /// <summary>Если true, этот клиент должен прочитать данные, отправленные другим клиентом</summary>
- public bool IsReading
- {
- get { return !this.IsWriting; }
- }
- /// <summary>Count of items in the stream.</summary>
- public int Count
- {
- get { return this.IsWriting ? this.writeData.Count : this.readData.Length; }
- }
- /// <summary>
- /// Creates a stream and initializes it. Used by PUN internally.
- /// </summary>
- public PhotonStream(bool write, object[] incomingData)
- {
- this.IsWriting = write;
- if (!write && incomingData != null)
- {
- this.readData = incomingData;
- }
- }
- public void SetReadStream(object[] incomingData, int pos = 0)
- {
- this.readData = incomingData;
- this.currentItem = pos;
- this.IsWriting = false;
- }
- internal void SetWriteStream(List<object> newWriteData, int pos = 0)
- {
- if (pos != newWriteData.Count)
- {
- throw new Exception("SetWriteStream failed, because count does not match position value. pos: "+ pos + " newWriteData.Count:" + newWriteData.Count);
- }
- this.writeData = newWriteData;
- this.currentItem = pos;
- this.IsWriting = true;
- }
- internal List<object> GetWriteStream()
- {
- return this.writeData;
- }
- [Obsolete("Either SET the writeData with an empty List or use Clear().")]
- internal void ResetWriteStream()
- {
- this.writeData.Clear();
- }
- /// <summary>Read next piece of data from the stream when IsReading is true.</summary>
- public object ReceiveNext()
- {
- if (this.IsWriting)
- {
- Debug.LogError("Error: you cannot read this stream that you are writing!");
- return null;
- }
- object obj = this.readData[this.currentItem];
- this.currentItem++;
- return obj;
- }
- /// <summary>Read next piece of data from the stream without advancing the "current" item.</summary>
- public object PeekNext()
- {
- if (this.IsWriting)
- {
- Debug.LogError("Error: you cannot read this stream that you are writing!");
- return null;
- }
- object obj = this.readData[this.currentItem];
- //this.currentItem++;
- return obj;
- }
- /// <summary>Add another piece of data to send it when IsWriting is true.</summary>
- public void SendNext(object obj)
- {
- if (!this.IsWriting)
- {
- Debug.LogError("Error: you cannot write/send to this stream that you are reading!");
- return;
- }
- this.writeData.Add(obj);
- }
- [Obsolete("writeData is a list now. Use and re-use it directly.")]
- public bool CopyToListAndClear(List<object> target)
- {
- if (!this.IsWriting) return false;
- target.AddRange(this.writeData);
- this.writeData.Clear();
- return true;
- }
- /// <summary>Turns the stream into a new object[].</summary>
- public object[] ToArray()
- {
- return this.IsWriting ? this.writeData.ToArray() : this.readData;
- }
- /// <summary>
- /// Will read or write the value, depending on the stream's IsWriting value.
- /// </summary>
- public void Serialize(ref bool myBool)
- {
- if (this.IsWriting)
- {
- this.writeData.Add(myBool);
- }
- else
- {
- if (this.readData.Length > this.currentItem)
- {
- myBool = (bool) this.readData[this.currentItem];
- this.currentItem++;
- }
- }
- }
- /// <summary>
- /// Will read or write the value, depending on the stream's IsWriting value.
- /// </summary>
- public void Serialize(ref int myInt)
- {
- if (this.IsWriting)
- {
- this.writeData.Add(myInt);
- }
- else
- {
- if (this.readData.Length > this.currentItem)
- {
- myInt = (int) this.readData[this.currentItem];
- this.currentItem++;
- }
- }
- }
- /// <summary>
- /// Will read or write the value, depending on the stream's IsWriting value.
- /// </summary>
- public void Serialize(ref string value)
- {
- if (this.IsWriting)
- {
- this.writeData.Add(value);
- }
- else
- {
- if (this.readData.Length > this.currentItem)
- {
- value = (string) this.readData[this.currentItem];
- this.currentItem++;
- }
- }
- }
- /// <summary>
- /// Will read or write the value, depending on the stream's IsWriting value.
- /// </summary>
- public void Serialize(ref char value)
- {
- if (this.IsWriting)
- {
- this.writeData.Add(value);
- }
- else
- {
- if (this.readData.Length > this.currentItem)
- {
- value = (char) this.readData[this.currentItem];
- this.currentItem++;
- }
- }
- }
- /// <summary>
- /// Will read or write the value, depending on the stream's IsWriting value.
- /// </summary>
- public void Serialize(ref short value)
- {
- if (this.IsWriting)
- {
- this.writeData.Add(value);
- }
- else
- {
- if (this.readData.Length > this.currentItem)
- {
- value = (short) this.readData[this.currentItem];
- this.currentItem++;
- }
- }
- }
- /// <summary>
- /// Will read or write the value, depending on the stream's IsWriting value.
- /// </summary>
- public void Serialize(ref float obj)
- {
- if (this.IsWriting)
- {
- this.writeData.Add(obj);
- }
- else
- {
- if (this.readData.Length > this.currentItem)
- {
- obj = (float) this.readData[this.currentItem];
- this.currentItem++;
- }
- }
- }
- /// <summary>
- /// Will read or write the value, depending on the stream's IsWriting value.
- /// </summary>
- public void Serialize(ref Player obj)
- {
- if (this.IsWriting)
- {
- this.writeData.Add(obj);
- }
- else
- {
- if (this.readData.Length > this.currentItem)
- {
- obj = (Player) this.readData[this.currentItem];
- this.currentItem++;
- }
- }
- }
- /// <summary>
- /// Will read or write the value, depending on the stream's IsWriting value.
- /// </summary>
- public void Serialize(ref Vector3 obj)
- {
- if (this.IsWriting)
- {
- this.writeData.Add(obj);
- }
- else
- {
- if (this.readData.Length > this.currentItem)
- {
- obj = (Vector3) this.readData[this.currentItem];
- this.currentItem++;
- }
- }
- }
- /// <summary>
- /// Will read or write the value, depending on the stream's IsWriting value.
- /// </summary>
- public void Serialize(ref Vector2 obj)
- {
- if (this.IsWriting)
- {
- this.writeData.Add(obj);
- }
- else
- {
- if (this.readData.Length > this.currentItem)
- {
- obj = (Vector2) this.readData[this.currentItem];
- this.currentItem++;
- }
- }
- }
- /// <summary>
- /// Will read or write the value, depending on the stream's IsWriting value.
- /// </summary>
- public void Serialize(ref Quaternion obj)
- {
- if (this.IsWriting)
- {
- this.writeData.Add(obj);
- }
- else
- {
- if (this.readData.Length > this.currentItem)
- {
- obj = (Quaternion) this.readData[this.currentItem];
- this.currentItem++;
- }
- }
- }
- }
- public class SceneManagerHelper
- {
- public static string ActiveSceneName
- {
- get
- {
- Scene s = SceneManager.GetActiveScene();
- return s.name;
- }
- }
- public static int ActiveSceneBuildIndex
- {
- get { return SceneManager.GetActiveScene().buildIndex; }
- }
- #if UNITY_EDITOR
- /// <summary>In Editor, we can access the active scene's name.</summary>
- public static string EditorActiveSceneName
- {
- get { return SceneManager.GetActiveScene().name; }
- }
- #endif
- }
Add Comment
Please, Sign In to add comment