double inventory screen
This commit is contained in:
@@ -1,631 +0,0 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using UnityEditor;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
[Serializable]
|
||||
public class Blackboard : Dictionary<string, object>, ISerializationCallbackReceiver
|
||||
{
|
||||
[Serializable]
|
||||
//struct represents the string key, and the serialized value from the dictionary.
|
||||
private struct SaveItem
|
||||
{
|
||||
public string key;
|
||||
public string value;
|
||||
public int index;
|
||||
|
||||
public SaveItem(string key, string val, int index)
|
||||
{
|
||||
this.key = key;
|
||||
this.value = val;
|
||||
this.index = index;
|
||||
}
|
||||
}
|
||||
|
||||
//All serialized items except for objects in a scene, which have to be handled separately.
|
||||
[SerializeField, HideInInspector]
|
||||
private List<SaveItem> saveItems;
|
||||
|
||||
//We need a different struct and list for Object references in scene :(
|
||||
[Serializable]
|
||||
private struct NonAssetSaveItem
|
||||
{
|
||||
public string key;
|
||||
public UnityEngine.Object obj;
|
||||
public int index;
|
||||
|
||||
public NonAssetSaveItem(string key, UnityEngine.Object obj, int index)
|
||||
{
|
||||
this.key = key;
|
||||
this.obj = obj;
|
||||
this.index = index;
|
||||
}
|
||||
}
|
||||
[SerializeField, HideInInspector]
|
||||
private List<NonAssetSaveItem> sceneObjectSaveItems;
|
||||
|
||||
/// <summary>
|
||||
/// Takes all of the keyvalue pairs from the Dictionary and stores them as Serializable lists.
|
||||
/// </summary>
|
||||
public void OnBeforeSerialize()
|
||||
{
|
||||
sceneObjectSaveItems = new List<NonAssetSaveItem>();
|
||||
saveItems = new List<SaveItem>();
|
||||
List<string> keys = this.Keys.ToList();
|
||||
List<object> values = this.Values.ToList();
|
||||
|
||||
for (int i = 0; i < Count; i++)
|
||||
{
|
||||
object value = values[i];
|
||||
string encode = "";
|
||||
|
||||
//Unhandled Enum Types
|
||||
if (value is Enum)
|
||||
{
|
||||
Enum enumValue = (Enum)value;
|
||||
Console.WriteLine("Enum Value: " + enumValue.ToString());
|
||||
encode = $"({value.GetType().AssemblyQualifiedName}){enumValue}";
|
||||
saveItems.Add(new SaveItem(keys[i], encode, i));
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (value)
|
||||
{
|
||||
case null: encode = "(null)"; break;
|
||||
case int: encode = "(int)" + ((int)value).ToString("F9"); break;
|
||||
case float: encode = "(float)" + ((float)value).ToString("F9"); break;
|
||||
case double: encode = "(double)" + ((double)value).ToString("F9"); break;
|
||||
case long: encode = "(long)" + ((long)value).ToString(); break;
|
||||
case string: encode = "(string)" + (string)value; break;
|
||||
case bool: encode = "(bool)" + (((bool)value) == true ? "true" : "false"); break;
|
||||
case Vector2Int: encode = "(Vector2Int)" + ((Vector2Int)value).ToString(); break;
|
||||
case Vector3Int: encode = "(Vector3Int)" + ((Vector3Int)value).ToString(); break;
|
||||
case Vector2: encode = "(Vector2)" + ((Vector2)value).ToString(); break;
|
||||
case Vector3: encode = "(Vector3)" + ((Vector3)value).ToString(); break;
|
||||
case Vector4: encode = "(Vector4)" + ((Vector4)value).ToString(); break;
|
||||
case Bounds: encode = "(Bounds)" + ((Bounds)value).ToString(); break;
|
||||
case Rect: encode = "(Rect)" + ((Rect)value).ToString("F9"); break;
|
||||
case Color: encode = "(Color)" + JsonUtility.ToJson((Color)value); break;
|
||||
case AnimationCurve: encode = "(AnimationCurve)" + Serializer.SerializeAnimationCurve((AnimationCurve)value); break;
|
||||
case Gradient: encode = "(Gradient)" + Serializer.SerializeGradient((Gradient)value); break;
|
||||
case UnityEngine.Object obj:
|
||||
string assetPath = Application.isEditor ? AssetDatabase.GetAssetPath(obj) : null;
|
||||
if (!string.IsNullOrEmpty(assetPath))
|
||||
{
|
||||
encode = "(UnityEngine.Object)" + assetPath;
|
||||
}
|
||||
else
|
||||
{
|
||||
sceneObjectSaveItems.Add(new NonAssetSaveItem(keys[i], obj, i));
|
||||
}
|
||||
break;
|
||||
//Try to serialize to JSON. May be empty if type is not supported
|
||||
default: encode = $"({value.GetType().AssemblyQualifiedName}){JsonUtility.ToJson(value)}"; break;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(encode))
|
||||
{
|
||||
saveItems.Add(new SaveItem(keys[i], encode, i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads the two lists back into the Dictionary, using the Merge Linked Lists method.
|
||||
/// </summary>
|
||||
public void OnAfterDeserialize()
|
||||
{
|
||||
this.Clear();
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
|
||||
//Ensure that the lists are not null to ensure no errors when accessing list.Count
|
||||
saveItems = saveItems == null ? new List<SaveItem>() : saveItems;
|
||||
sceneObjectSaveItems = sceneObjectSaveItems == null ? new List<NonAssetSaveItem>() : sceneObjectSaveItems;
|
||||
|
||||
while (i < saveItems.Count && j < sceneObjectSaveItems.Count)
|
||||
{
|
||||
if (saveItems[i].index < sceneObjectSaveItems[j].index)
|
||||
{
|
||||
string key = saveItems[i].key;
|
||||
int openIndex = saveItems[i].value.IndexOf('(');
|
||||
int closeIndex = saveItems[i].value.IndexOf(')');
|
||||
string contentType = saveItems[i].value.Substring(openIndex + 1, closeIndex - openIndex - 1);
|
||||
string encode = saveItems[i].value.Substring(closeIndex + 1);
|
||||
DeserializeItem(contentType, key, encode);
|
||||
i++;
|
||||
}
|
||||
else
|
||||
{
|
||||
Add(sceneObjectSaveItems[j].key, sceneObjectSaveItems[j].obj);
|
||||
j++;
|
||||
}
|
||||
}
|
||||
|
||||
for(; i < saveItems.Count;i++)
|
||||
{
|
||||
string key = saveItems[i].key;
|
||||
int openIndex = saveItems[i].value.IndexOf('(');
|
||||
int closeIndex = saveItems[i].value.IndexOf(')');
|
||||
string contentType = saveItems[i].value.Substring(openIndex + 1, closeIndex - openIndex - 1);
|
||||
string encode = saveItems[i].value.Substring(closeIndex + 1);
|
||||
DeserializeItem(contentType, key, encode);
|
||||
}
|
||||
|
||||
for (; j < sceneObjectSaveItems.Count; j++)
|
||||
{
|
||||
Add(sceneObjectSaveItems[j].key, sceneObjectSaveItems[j].obj);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Takes the key and encoded string from a serialized item and adds it back into the dictionary.
|
||||
/// </summary>
|
||||
private void DeserializeItem(string contentType, string key, string encodedValue)
|
||||
{
|
||||
switch (contentType)
|
||||
{
|
||||
case "null": Add(key, null); return;
|
||||
case "int": Add(key, (int)int.Parse(encodedValue)); return;
|
||||
case "float": Add(key, (float)float.Parse(encodedValue)); return;
|
||||
case "double": Add(key, (double)double.Parse(encodedValue)); return;
|
||||
case "long": Add(key, (long)long.Parse(encodedValue)); return;
|
||||
case "string": Add(key, (string)encodedValue); return;
|
||||
case "bool": Add(key, (bool)(encodedValue == "true" ? true : false)); return;
|
||||
case "Vector2": Add(key, Serializer.ParseVector2(encodedValue)); return;
|
||||
case "Vector3": Add(key, Serializer.ParseVector3(encodedValue)); return;
|
||||
case "Vector2Int": Add(key, Serializer.ParseVector2Int(encodedValue)); return;
|
||||
case "Vector3Int": Add(key, Serializer.ParseVector3Int(encodedValue)); return;
|
||||
case "Vector4": Add(key, Serializer.ParseVector4(encodedValue)); return;
|
||||
case "Bounds": Add(key, Serializer.ParseBounds(encodedValue)); return;
|
||||
case "Rect": Add(key, Serializer.ParseRect(encodedValue)); return;
|
||||
case "Color": Add(key, JsonUtility.FromJson<Color>(encodedValue)); return;
|
||||
case "AnimationCurve": Add(key, Serializer.DeserializeAnimationCurve(encodedValue)); return;
|
||||
case "Gradient": Add(key, Serializer.DeserializeGradient(encodedValue)); return;
|
||||
case "UnityEngine.Object":
|
||||
if(Application.isEditor)
|
||||
{
|
||||
EditorApplication.delayCall += () => Add(key, AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(encodedValue));
|
||||
}
|
||||
else
|
||||
{
|
||||
Add(key, Resources.Load(encodedValue));
|
||||
}
|
||||
return;
|
||||
default: break;
|
||||
}
|
||||
|
||||
//Different process for enums (of any type)
|
||||
if(Serializer.EnumDeserialize(contentType, encodedValue, out object enumValue))
|
||||
{
|
||||
Add(key, enumValue);
|
||||
}
|
||||
|
||||
//Tries to de-serialize a struct or class using JsonUtility.FromJson
|
||||
if(Serializer.TryDeserializeJSON(contentType, encodedValue, out object result))
|
||||
{
|
||||
Add(key, result);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetOrAdd(string key, object ob)
|
||||
{
|
||||
if (this.ContainsKey(key))
|
||||
{
|
||||
this[key] = ob;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Add(key, ob);
|
||||
}
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
private static class Serializer
|
||||
{
|
||||
#region GradientSerialization
|
||||
[System.Serializable]
|
||||
private class SerializableGradient
|
||||
{
|
||||
public SerializableColorKey[] colorKeys;
|
||||
public SerializableAlphaKey[] alphaKeys;
|
||||
public GradientMode mode;
|
||||
|
||||
public SerializableGradient(Gradient gradient)
|
||||
{
|
||||
colorKeys = new SerializableColorKey[gradient.colorKeys.Length];
|
||||
for (int i = 0; i < gradient.colorKeys.Length; i++)
|
||||
{
|
||||
colorKeys[i] = new SerializableColorKey(gradient.colorKeys[i]);
|
||||
}
|
||||
|
||||
alphaKeys = new SerializableAlphaKey[gradient.alphaKeys.Length];
|
||||
for (int i = 0; i < gradient.alphaKeys.Length; i++)
|
||||
{
|
||||
alphaKeys[i] = new SerializableAlphaKey(gradient.alphaKeys[i]);
|
||||
}
|
||||
|
||||
mode = gradient.mode;
|
||||
}
|
||||
|
||||
public Gradient ToGradient()
|
||||
{
|
||||
Gradient gradient = new Gradient();
|
||||
GradientColorKey[] gradientColorKeys = new GradientColorKey[colorKeys.Length];
|
||||
for (int i = 0; i < colorKeys.Length; i++)
|
||||
{
|
||||
gradientColorKeys[i] = colorKeys[i].ToGradientColorKey();
|
||||
}
|
||||
|
||||
GradientAlphaKey[] gradientAlphaKeys = new GradientAlphaKey[alphaKeys.Length];
|
||||
for (int i = 0; i < alphaKeys.Length; i++)
|
||||
{
|
||||
gradientAlphaKeys[i] = alphaKeys[i].ToGradientAlphaKey();
|
||||
}
|
||||
|
||||
gradient.SetKeys(gradientColorKeys, gradientAlphaKeys);
|
||||
gradient.mode = mode;
|
||||
|
||||
return gradient;
|
||||
}
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
private class SerializableColorKey
|
||||
{
|
||||
public Color color;
|
||||
public float time;
|
||||
|
||||
public SerializableColorKey(GradientColorKey colorKey)
|
||||
{
|
||||
color = colorKey.color;
|
||||
time = colorKey.time;
|
||||
}
|
||||
|
||||
public GradientColorKey ToGradientColorKey()
|
||||
{
|
||||
return new GradientColorKey(color, time);
|
||||
}
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
private class SerializableAlphaKey
|
||||
{
|
||||
public float alpha;
|
||||
public float time;
|
||||
|
||||
public SerializableAlphaKey(GradientAlphaKey alphaKey)
|
||||
{
|
||||
alpha = alphaKey.alpha;
|
||||
time = alphaKey.time;
|
||||
}
|
||||
|
||||
public GradientAlphaKey ToGradientAlphaKey()
|
||||
{
|
||||
return new GradientAlphaKey(alpha, time);
|
||||
}
|
||||
}
|
||||
|
||||
public static string SerializeGradient(Gradient gradient)
|
||||
{
|
||||
SerializableGradient serializableGradient = new SerializableGradient(gradient);
|
||||
return JsonUtility.ToJson(serializableGradient);
|
||||
}
|
||||
|
||||
public static Gradient DeserializeGradient(string json)
|
||||
{
|
||||
SerializableGradient serializableGradient = JsonUtility.FromJson<SerializableGradient>(json);
|
||||
|
||||
if (serializableGradient != null)
|
||||
{
|
||||
return serializableGradient.ToGradient();
|
||||
}
|
||||
|
||||
Debug.LogError("Failed to deserialize Gradient from JSON: " + json);
|
||||
return new Gradient(); // Return a default Gradient or handle the error as needed
|
||||
}
|
||||
#endregion
|
||||
#region AnimationCurveSerialization
|
||||
[System.Serializable]
|
||||
private struct SerializableKeyframe
|
||||
{
|
||||
public float time;
|
||||
public float value;
|
||||
public float inTangent;
|
||||
public float outTangent;
|
||||
|
||||
public SerializableKeyframe(Keyframe keyframe)
|
||||
{
|
||||
time = keyframe.time;
|
||||
value = keyframe.value;
|
||||
inTangent = keyframe.inTangent;
|
||||
outTangent = keyframe.outTangent;
|
||||
}
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
private struct SerializableAnimationCurve
|
||||
{
|
||||
public WrapMode preWrapMode;
|
||||
public WrapMode postWrapMode;
|
||||
public SerializableKeyframe[] keys;
|
||||
|
||||
public SerializableAnimationCurve(AnimationCurve curve)
|
||||
{
|
||||
preWrapMode = curve.preWrapMode;
|
||||
postWrapMode = curve.postWrapMode;
|
||||
keys = new SerializableKeyframe[curve.length];
|
||||
for (int i = 0; i < curve.length; i++)
|
||||
{
|
||||
keys[i] = new SerializableKeyframe(curve[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serializes an AnimationCurve to a JSON string.
|
||||
/// </summary>
|
||||
public static string SerializeAnimationCurve(AnimationCurve curve)
|
||||
{
|
||||
SerializableAnimationCurve serializableCurve = new SerializableAnimationCurve(curve);
|
||||
string json = JsonUtility.ToJson(serializableCurve);
|
||||
return json;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Produces an AnimationCurve from a json string.
|
||||
/// </summary>
|
||||
public static AnimationCurve DeserializeAnimationCurve(string json)
|
||||
{
|
||||
SerializableAnimationCurve serializableCurve = JsonUtility.FromJson<SerializableAnimationCurve>(json);
|
||||
|
||||
Keyframe[] keyframes = new Keyframe[serializableCurve.keys.Length];
|
||||
for (int i = 0; i < keyframes.Length; i++)
|
||||
{
|
||||
keyframes[i] = new Keyframe(
|
||||
serializableCurve.keys[i].time,
|
||||
serializableCurve.keys[i].value,
|
||||
serializableCurve.keys[i].inTangent,
|
||||
serializableCurve.keys[i].outTangent
|
||||
);
|
||||
}
|
||||
|
||||
AnimationCurve curve = new AnimationCurve(keyframes);
|
||||
curve.postWrapMode = serializableCurve.postWrapMode;
|
||||
curve.preWrapMode = serializableCurve.postWrapMode;
|
||||
return curve;
|
||||
}
|
||||
#endregion
|
||||
#region VectorSerialization
|
||||
public static Vector2 ParseVector2(string vectorString)
|
||||
{
|
||||
vectorString = vectorString.Replace("(", "").Replace(")", "");
|
||||
string[] components = vectorString.Split(',');
|
||||
|
||||
if (components.Length == 2 &&
|
||||
float.TryParse(components[0], out float x) &&
|
||||
float.TryParse(components[1], out float y))
|
||||
{
|
||||
return new Vector2(x, y);
|
||||
}
|
||||
|
||||
Debug.LogError("Failed to parse Vector2 from string: " + vectorString);
|
||||
return Vector2.zero;
|
||||
}
|
||||
|
||||
public static Vector2Int ParseVector2Int(string vectorString)
|
||||
{
|
||||
vectorString = vectorString.Replace("(", "").Replace(")", "");
|
||||
string[] components = vectorString.Split(',');
|
||||
|
||||
if (components.Length == 2 &&
|
||||
int.TryParse(components[0], out int x) &&
|
||||
int.TryParse(components[1], out int y))
|
||||
{
|
||||
return new Vector2Int(x, y);
|
||||
}
|
||||
|
||||
Debug.LogError("Failed to parse Vector2 from string: " + vectorString);
|
||||
return Vector2Int.zero;
|
||||
}
|
||||
|
||||
public static Vector3 ParseVector3(string vectorString)
|
||||
{
|
||||
vectorString = vectorString.Replace("(", "").Replace(")", "");
|
||||
string[] components = vectorString.Split(',');
|
||||
|
||||
if (components.Length == 3 &&
|
||||
float.TryParse(components[0], out float x) &&
|
||||
float.TryParse(components[1], out float y) &&
|
||||
float.TryParse(components[2], out float z))
|
||||
{
|
||||
return new Vector3(x, y, z);
|
||||
}
|
||||
|
||||
Debug.LogError("Failed to parse Vector3 from string: " + vectorString);
|
||||
return Vector3.zero;
|
||||
}
|
||||
|
||||
public static Vector3Int ParseVector3Int(string vectorString)
|
||||
{
|
||||
vectorString = vectorString.Replace("(", "").Replace(")", "");
|
||||
string[] components = vectorString.Split(',');
|
||||
|
||||
if (components.Length == 3 &&
|
||||
int.TryParse(components[0], out int x) &&
|
||||
int.TryParse(components[1], out int y) &&
|
||||
int.TryParse(components[2], out int z))
|
||||
{
|
||||
return new Vector3Int(x, y, z);
|
||||
}
|
||||
|
||||
Debug.LogError("Failed to parse Vector3Int from string: " + vectorString);
|
||||
return Vector3Int.zero;
|
||||
}
|
||||
|
||||
public static Vector4 ParseVector4(string vectorString)
|
||||
{
|
||||
vectorString = vectorString.Replace("(", "").Replace(")", "");
|
||||
string[] components = vectorString.Split(',');
|
||||
|
||||
if (components.Length == 4 &&
|
||||
float.TryParse(components[0], out float x) &&
|
||||
float.TryParse(components[1], out float y) &&
|
||||
float.TryParse(components[2], out float z) &&
|
||||
float.TryParse(components[3], out float w))
|
||||
{
|
||||
return new Vector4(x, y, z, w);
|
||||
}
|
||||
|
||||
Debug.LogError("Failed to parse Vector4 from string: " + vectorString);
|
||||
return Vector4.zero;
|
||||
}
|
||||
#endregion
|
||||
#region BoundsSerialization
|
||||
/// <summary>
|
||||
/// Produces a Bounds object from the result of Bounds.ToString(). Returns a Bounds with all zero values if unable to parse.
|
||||
/// </summary>
|
||||
public static Bounds ParseBounds(string boundsString)
|
||||
{
|
||||
// Remove parentheses and labels from the string
|
||||
boundsString = Regex.Replace(boundsString, @"[^\d\.\-,]", "");
|
||||
|
||||
string[] components = boundsString.Split(',');
|
||||
|
||||
if (components.Length == 6 &&
|
||||
float.TryParse(components[0], out float center_x) &&
|
||||
float.TryParse(components[1], out float center_y) &&
|
||||
float.TryParse(components[2], out float center_z) &&
|
||||
float.TryParse(components[3], out float extent_x) &&
|
||||
float.TryParse(components[4], out float extent_y) &&
|
||||
float.TryParse(components[5], out float extent_z))
|
||||
{
|
||||
Vector3 center = new Vector3(center_x, center_y, center_z);
|
||||
Vector3 size = new Vector3(extent_x, extent_y, extent_z) * 2f;
|
||||
return new Bounds(center, size);
|
||||
}
|
||||
|
||||
Debug.LogWarning("Failed to parse Bounds from string: " + boundsString);
|
||||
return new Bounds(Vector3.zero, Vector3.zero);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Produces a BoundsInt object from the result of BoundsInt.ToString(). Returns a Bounds with all zero values if unable to parse.
|
||||
/// </summary>
|
||||
public static BoundsInt ParseBoundsInt(string boundsString)
|
||||
{
|
||||
// Remove parentheses and labels and any unwanted decimals from the string
|
||||
boundsString = Regex.Replace(boundsString, @"[^\d\-,]", "");
|
||||
|
||||
string[] components = boundsString.Split(',');
|
||||
|
||||
if (components.Length == 6 &&
|
||||
int.TryParse(components[0], out int center_x) &&
|
||||
int.TryParse(components[1], out int center_y) &&
|
||||
int.TryParse(components[2], out int center_z) &&
|
||||
int.TryParse(components[3], out int extent_x) &&
|
||||
int.TryParse(components[4], out int extent_y) &&
|
||||
int.TryParse(components[5], out int extent_z))
|
||||
{
|
||||
Vector3Int center = new Vector3Int(center_x, center_y, center_z);
|
||||
Vector3Int size = new Vector3Int(extent_x, extent_y, extent_z) * 2;
|
||||
return new BoundsInt(center, size);
|
||||
}
|
||||
|
||||
Debug.LogWarning("Failed to parse BoundsInt from string: " + boundsString);
|
||||
return new BoundsInt(Vector3Int.zero, Vector3Int.zero);
|
||||
}
|
||||
#endregion
|
||||
#region RectSerialization
|
||||
|
||||
/// <summary>
|
||||
/// Takes the string result of Rect.ToString() and produces the original Rect. Returns a zero-rect if unable to parse.
|
||||
/// </summary>
|
||||
public static Rect ParseRect(string rectString)
|
||||
{
|
||||
// Remove parentheses and labels from the string
|
||||
rectString = Regex.Replace(rectString, @"[^\d\.\-,]", "");
|
||||
|
||||
string[] components = rectString.Split(',');
|
||||
|
||||
if (components.Length == 4 &&
|
||||
float.TryParse(components[0], out float x) &&
|
||||
float.TryParse(components[1], out float y) &&
|
||||
float.TryParse(components[2], out float width) &&
|
||||
float.TryParse(components[3], out float height))
|
||||
{
|
||||
Rect rect = new Rect(x, y, width, height);
|
||||
return rect;
|
||||
}
|
||||
|
||||
Debug.LogWarning("Failed to parse Rect from string: " + rectString);
|
||||
return new Rect(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Takes the string result of RectInt.ToString() and produces the original RectInt. Returns a zero-rect if unable to parse.
|
||||
/// </summary>
|
||||
public static RectInt ParseRectInt(string rectString)
|
||||
{
|
||||
// Remove parentheses and labels from the string
|
||||
rectString = Regex.Replace(rectString, @"[^\d\-,]", "");
|
||||
|
||||
string[] components = rectString.Split(',');
|
||||
|
||||
if (components.Length == 4 &&
|
||||
int.TryParse(components[0], out int x) &&
|
||||
int.TryParse(components[1], out int y) &&
|
||||
int.TryParse(components[2], out int width) &&
|
||||
int.TryParse(components[3], out int height))
|
||||
{
|
||||
RectInt rect = new RectInt(x, y, width, height);
|
||||
return rect;
|
||||
}
|
||||
|
||||
Debug.LogWarning("Failed to parse RectInt from string: " + rectString);
|
||||
return new RectInt(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Takes the type, encoded as string, and the enum value and produces an Enum of the proper type.
|
||||
/// </summary>
|
||||
public static bool EnumDeserialize(string contentType, string encodedValue, out object enumValue)
|
||||
{
|
||||
Type type = Type.GetType(contentType);
|
||||
if (type != null && type.IsEnum)
|
||||
{
|
||||
if (Enum.TryParse(type, encodedValue, out object enumIntermediateValue))
|
||||
{
|
||||
enumValue = Convert.ChangeType(enumIntermediateValue, type);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
enumValue = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Takes as input a string to be converted to a type, which is used to produce a Deserialized object.
|
||||
/// </summary>
|
||||
public static bool TryDeserializeJSON(string contentType, string json, out object result)
|
||||
{
|
||||
Type type = Type.GetType(contentType);
|
||||
if (type != null)
|
||||
{
|
||||
result = JsonUtility.FromJson(json, type);
|
||||
if (result != null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
result = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5514fe3cae16a7e4bb756eb9393ae1fd
|
||||
@@ -9,11 +9,7 @@ public class GameData
|
||||
|
||||
public PlayerData player = new PlayerData();
|
||||
|
||||
public List<ShelfData> shelves = new List<ShelfData>();
|
||||
public StorageData storage = new StorageData();
|
||||
|
||||
// the values defined in this constructor will be the default values
|
||||
// the game starts with when there's no data to load
|
||||
public GameData()
|
||||
{
|
||||
}
|
||||
public List<ShelfData> shelves = new List<ShelfData>();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
using System;
|
||||
|
||||
[System.Serializable]
|
||||
public class StorageData
|
||||
{
|
||||
public InventoryData inventory = new InventoryData();
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9691713479005104ea634e90c3338df3
|
||||
@@ -1,11 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9dc07c3462db23d45a4aa37b7d15244c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
guid: 9dc07c3462db23d45a4aa37b7d15244c
|
||||
@@ -1,5 +0,0 @@
|
||||
public interface IInventoryHolder
|
||||
{
|
||||
public Inventory Inventory { get; }
|
||||
public string Name { get; }
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using UnityEngine;
|
||||
|
||||
// class, not interface so Unity shows fields in Editor
|
||||
public abstract class InventoryHolder : MonoBehaviour
|
||||
{
|
||||
public abstract Inventory Inventory { get; }
|
||||
public abstract string Name { get; }
|
||||
}
|
||||
@@ -1,12 +1,12 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class Player : MonoBehaviour, IInventoryHolder, IDataPersistence<GameData>
|
||||
public class Player : InventoryHolder, IDataPersistence<GameData>
|
||||
{
|
||||
private Inventory inventory = new Inventory(50);
|
||||
|
||||
public Inventory Inventory => inventory;
|
||||
public override Inventory Inventory => inventory;
|
||||
|
||||
public string Name => gameObject.name;
|
||||
public override string Name => gameObject.name;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class Shelf : MonoBehaviour, IInventoryHolder, IDataPersistence<ShelfData>
|
||||
public class Shelf : InventoryHolder, IDataPersistence<ShelfData>
|
||||
{
|
||||
private Inventory inventory = new Inventory(50);
|
||||
|
||||
public Inventory Inventory => inventory;
|
||||
public override Inventory Inventory => inventory;
|
||||
|
||||
public string Name => type;
|
||||
public override string Name => type;
|
||||
|
||||
public string type = "shelf";
|
||||
|
||||
|
||||
@@ -5,6 +5,8 @@ using UnityEngine;
|
||||
|
||||
public class Shelves : MonoBehaviour, IDataPersistence<GameData>
|
||||
{
|
||||
public Storage storage;
|
||||
|
||||
public GameObject ShelfPrefab;
|
||||
|
||||
private List<Shelf> items = new List<Shelf>();
|
||||
@@ -42,6 +44,9 @@ public class Shelves : MonoBehaviour, IDataPersistence<GameData>
|
||||
Shelf shelf = instance.GetComponent<Shelf>();
|
||||
shelf.LoadData(data);
|
||||
|
||||
InventoryScreenInvoker inventoryScreen = instance.GetComponent<InventoryScreenInvoker>();
|
||||
inventoryScreen.SourceInventory = storage;
|
||||
|
||||
items.Add(shelf);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class Storage : InventoryHolder, IDataPersistence<GameData>
|
||||
{
|
||||
private Inventory inventory = new Inventory(-1);
|
||||
|
||||
public override Inventory Inventory => inventory;
|
||||
|
||||
public override string Name => gameObject.name;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
// Add("Лимонад", 5);
|
||||
// Add("Тортик", 22);
|
||||
// Add("Спичка", 100);
|
||||
}
|
||||
|
||||
// private void Add(string key, int value)
|
||||
// {
|
||||
// int added = inventory.inventory.Add(key, value, true);
|
||||
// Debug.Log($"{key}: добавлено {added} из {value}");
|
||||
// if (added < value) {
|
||||
// int dropped = DropToInventory.inventory.Add(key, value - added);
|
||||
// Debug.Log($"{key}: брошено {dropped} из {value - added}");
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
public void LoadData(GameData data)
|
||||
{
|
||||
inventory.LoadData(data.storage.inventory);
|
||||
}
|
||||
|
||||
public void SaveData(GameData data)
|
||||
{
|
||||
inventory.SaveData(data.storage.inventory);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 003acbbf759b3bc4ea37150bcd331f99
|
||||
@@ -14,13 +14,13 @@ public class InfoBox : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
|
||||
|
||||
private UIDocument uiDocument;
|
||||
private Camera mainCamera;
|
||||
private IInventoryHolder inventoryHolder;
|
||||
private InventoryHolder inventoryHolder;
|
||||
private Label infoBox;
|
||||
|
||||
void Start()
|
||||
{
|
||||
mainCamera = Camera.main;
|
||||
inventoryHolder = GetComponent<IInventoryHolder>();
|
||||
inventoryHolder = GetComponent<InventoryHolder>();
|
||||
|
||||
uiDocument = gameObject.AddComponent<UIDocument>();
|
||||
uiDocument.panelSettings = uiPanelSettings;
|
||||
|
||||
@@ -1,39 +1,42 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
public class InventoryScreen : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
private VisualTreeAsset uiInventoryAsset;
|
||||
|
||||
[SerializeField]
|
||||
private VisualTreeAsset uiItemAsset;
|
||||
|
||||
private UIDocument uiDocument;
|
||||
|
||||
private VisualElement root;
|
||||
private VisualElement itemList;
|
||||
private Label headerLabel;
|
||||
private Label footerLabel;
|
||||
private VisualElement sourceInventory;
|
||||
private VisualElement targetInventory;
|
||||
|
||||
private IInventoryHolder inventoryHolder;
|
||||
private IInventoryHolder targetInventoryHolder;
|
||||
// private InventoryHolder inventoryHolder;
|
||||
// private InventoryHolder targetInventoryHolder;
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
uiDocument = GetComponent<UIDocument>();
|
||||
|
||||
root = uiDocument.rootVisualElement;
|
||||
var inventory = root.Q("Inventory");
|
||||
sourceInventory = root.Q("SourceInventory");
|
||||
sourceInventory.style.display = DisplayStyle.None;
|
||||
targetInventory = root.Q("TargetInventory");
|
||||
targetInventory.style.display = DisplayStyle.None;
|
||||
|
||||
root.style.display = DisplayStyle.None;
|
||||
|
||||
itemList = inventory.Q("Inventory").Q("ContainerScroll");
|
||||
headerLabel = inventory.Q<Label>("Header");
|
||||
footerLabel = inventory.Q<Label>("Footer");
|
||||
Hide();
|
||||
|
||||
root.RegisterCallback<ClickEvent>(OnRootClicked);
|
||||
inventory.RegisterCallback<ClickEvent>(evt => evt.StopPropagation());
|
||||
sourceInventory.RegisterCallback<ClickEvent>(evt => evt.StopPropagation());
|
||||
targetInventory.RegisterCallback<ClickEvent>(evt => evt.StopPropagation());
|
||||
}
|
||||
|
||||
private void OnRootClicked(ClickEvent evt)
|
||||
@@ -52,14 +55,37 @@ public class InventoryScreen : MonoBehaviour
|
||||
root.style.display = DisplayStyle.None;
|
||||
}
|
||||
|
||||
public void Display(IInventoryHolder inventory, IInventoryHolder targetInventory = null)
|
||||
public void Display(InventoryHolder sourceInventory, InventoryHolder targetInventory = null)
|
||||
{
|
||||
this.inventoryHolder = inventory;
|
||||
this.targetInventoryHolder = inventory;
|
||||
ShowInventory(this.sourceInventory, sourceInventory);
|
||||
ShowInventory(this.targetInventory, targetInventory);
|
||||
|
||||
Show();
|
||||
}
|
||||
|
||||
public void ShowInventory(VisualElement inventory, InventoryHolder holder)
|
||||
{
|
||||
inventory.Clear();
|
||||
|
||||
if (holder == null)
|
||||
{
|
||||
inventory.style.display = DisplayStyle.None;
|
||||
return;
|
||||
}
|
||||
inventory.style.display = DisplayStyle.Flex;
|
||||
|
||||
uiInventoryAsset.CloneTree(inventory);
|
||||
|
||||
// TemplateContainer inventory = uiInventoryAsset.Instantiate();
|
||||
// container.Add(inventory);
|
||||
|
||||
var itemList = inventory.Q("Inventory").Q("ContainerScroll");
|
||||
var headerLabel = inventory.Q<Label>("Header");
|
||||
var footerLabel = inventory.Q<Label>("Footer");
|
||||
|
||||
itemList.Clear();
|
||||
|
||||
foreach (var item in inventory.Inventory.Items)
|
||||
foreach (var item in holder.Inventory.Items)
|
||||
{
|
||||
var child = uiItemAsset.Instantiate();
|
||||
// child.AddToClassList(".item");
|
||||
@@ -68,9 +94,7 @@ public class InventoryScreen : MonoBehaviour
|
||||
itemList.Add(child);
|
||||
}
|
||||
|
||||
headerLabel.text = inventory.Name;
|
||||
footerLabel.text = $"Свободно: {inventory.Inventory.Free}";
|
||||
|
||||
Show();
|
||||
headerLabel.text = holder.Name;
|
||||
footerLabel.text = $"Свободно: {holder.Inventory.Free}";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,9 @@ using UnityEngine.UIElements;
|
||||
|
||||
public class InventoryScreenInvoker : MonoBehaviour, IPointerClickHandler
|
||||
{
|
||||
public InventoryHolder SourceInventory;
|
||||
public InventoryHolder TargetInventory;
|
||||
|
||||
public void OnPointerClick(PointerEventData eventData)
|
||||
{
|
||||
Show();
|
||||
@@ -19,13 +22,6 @@ public class InventoryScreenInvoker : MonoBehaviour, IPointerClickHandler
|
||||
return;
|
||||
}
|
||||
|
||||
var inventory = GetComponent<IInventoryHolder>();
|
||||
if (inventory == null)
|
||||
{
|
||||
Debug.Log("Cannot show inventory screen on an object without an inventory");
|
||||
return;
|
||||
}
|
||||
|
||||
inventoryScreen.Display(inventory);
|
||||
inventoryScreen.Display(SourceInventory, TargetInventory);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user