Almost done with created levels menu
This commit is contained in:
@@ -1014,7 +1014,6 @@ GameObject:
|
||||
- component: {fileID: 310338051}
|
||||
- component: {fileID: 310338050}
|
||||
- component: {fileID: 310338049}
|
||||
- component: {fileID: 310338048}
|
||||
m_Layer: 5
|
||||
m_Name: BackButton
|
||||
m_TagString: Untagged
|
||||
@@ -1042,19 +1041,6 @@ RectTransform:
|
||||
m_AnchoredPosition: {x: 60, y: -60}
|
||||
m_SizeDelta: {x: 100, y: 100}
|
||||
m_Pivot: {x: 0.5, y: 0.5}
|
||||
--- !u!114 &310338048
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 310338046}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 804df76422d8b45cfb9f80c2b6fdc795, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier: Assembly-CSharp::ButtonToScene
|
||||
sceneName: CreatorLayerMenu
|
||||
--- !u!114 &310338049
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
@@ -2625,7 +2611,7 @@ MonoBehaviour:
|
||||
m_CharacterValidation: 0
|
||||
m_RegexValue:
|
||||
m_GlobalPointSize: 14
|
||||
m_CharacterLimit: 0
|
||||
m_CharacterLimit: 16
|
||||
m_OnEndEdit:
|
||||
m_PersistentCalls:
|
||||
m_Calls: []
|
||||
@@ -2803,7 +2789,7 @@ MonoBehaviour:
|
||||
m_CharacterValidation: 0
|
||||
m_RegexValue:
|
||||
m_GlobalPointSize: 14
|
||||
m_CharacterLimit: 0
|
||||
m_CharacterLimit: 192
|
||||
m_OnEndEdit:
|
||||
m_PersistentCalls:
|
||||
m_Calls: []
|
||||
@@ -3842,9 +3828,11 @@ MonoBehaviour:
|
||||
m_Name:
|
||||
m_EditorClassIdentifier: Assembly-CSharp::CreatedLevelsMenu
|
||||
backButton: {fileID: 310338050}
|
||||
defaultLayer: {fileID: 253947627}
|
||||
defaultCreateButton: {fileID: 79404069}
|
||||
defaultScrollContent: {fileID: 316375077}
|
||||
defaultSampleLevel: {fileID: 822390386}
|
||||
levelLayer: {fileID: 692130939}
|
||||
levelDeleteButton: {fileID: 602857196}
|
||||
levelNameInput: {fileID: 1195607014}
|
||||
levelDescriptionInput: {fileID: 1249771697}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json.Linq;
|
||||
@@ -21,7 +22,7 @@ public class BazookaManager : MonoBehaviour
|
||||
if (!firstLoadDone)
|
||||
{
|
||||
firstLoadDone = true;
|
||||
Load();
|
||||
saveFile = Load("savefile.json");
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -32,20 +33,20 @@ public class BazookaManager : MonoBehaviour
|
||||
|
||||
void OnApplicationQuit()
|
||||
{
|
||||
Save();
|
||||
Save("savefile.json", saveFile);
|
||||
}
|
||||
|
||||
void OnApplicationPause(bool pause)
|
||||
{
|
||||
if (pause)
|
||||
{
|
||||
Save();
|
||||
Save("savefile.json", saveFile);
|
||||
}
|
||||
}
|
||||
|
||||
public void Load()
|
||||
public JObject Load(String pathSuffix)
|
||||
{
|
||||
string path = Path.Join(Application.persistentDataPath, "savefile.json");
|
||||
string path = Path.Join(Application.persistentDataPath, pathSuffix);
|
||||
if (!File.Exists(path))
|
||||
{
|
||||
File.Create(path).Dispose();
|
||||
@@ -55,24 +56,21 @@ public class BazookaManager : MonoBehaviour
|
||||
try
|
||||
{
|
||||
var tempSaveFile = JObject.Parse(File.ReadAllText(path));
|
||||
if (tempSaveFile != null) saveFile = tempSaveFile;
|
||||
return tempSaveFile;
|
||||
}
|
||||
catch
|
||||
{
|
||||
Application.Quit();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
if (saveFile["version"] == null || saveFile["version"].ToString() != "0")
|
||||
{
|
||||
Application.Quit();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void Save()
|
||||
public void Save(String pathSuffix, JObject data)
|
||||
{
|
||||
string path = Path.Join(Application.persistentDataPath, "savefile.json");
|
||||
string path = Path.Join(Application.persistentDataPath, pathSuffix);
|
||||
using var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);
|
||||
var encoded = Encoding.UTF8.GetBytes(saveFile.ToString(Newtonsoft.Json.Formatting.Indented));
|
||||
var encoded = Encoding.UTF8.GetBytes(data.ToString(Newtonsoft.Json.Formatting.Indented));
|
||||
fileStream.Write(encoded, 0, encoded.Length);
|
||||
fileStream.Flush(true);
|
||||
}
|
||||
@@ -83,6 +81,21 @@ public class BazookaManager : MonoBehaviour
|
||||
{
|
||||
["version"] = "0"
|
||||
};
|
||||
Save();
|
||||
Save("savefile.json", saveFile);
|
||||
}
|
||||
|
||||
//levels
|
||||
|
||||
public void SetCreatedLevels(JArray value)
|
||||
{
|
||||
if (saveFile["levels"] == null) saveFile["levels"] = new JObject();
|
||||
saveFile["levels"]["createdLevels"] = value;
|
||||
}
|
||||
|
||||
public JArray GetCreatedLevels()
|
||||
{
|
||||
if (saveFile["levels"] == null) return new JArray();
|
||||
if (saveFile["levels"]["createdLevels"] == null) return new JArray();
|
||||
return JArray.Parse(saveFile["levels"]["createdLevels"].ToString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,24 +1,171 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class CreatedLevelsMenu : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Button backButton;
|
||||
|
||||
[SerializeField] private GameObject defaultLayer;
|
||||
[SerializeField] private Button defaultCreateButton;
|
||||
[SerializeField] private GameObject defaultScrollContent;
|
||||
[SerializeField] private GameObject defaultSampleLevel;
|
||||
|
||||
[SerializeField] private GameObject levelLayer;
|
||||
[SerializeField] private Button levelDeleteButton;
|
||||
[SerializeField] private TMP_InputField levelNameInput;
|
||||
[SerializeField] private TMP_InputField levelDescriptionInput;
|
||||
[SerializeField] private Button levelEditLevelButton;
|
||||
[SerializeField] private Button levelPlaytestButton;
|
||||
[SerializeField] private Button levelShareButton;
|
||||
private string levelUuid;
|
||||
|
||||
void Start()
|
||||
{
|
||||
|
||||
ResetLevelView();
|
||||
levelDescriptionInput.textComponent.textWrappingMode = TextWrappingModes.Normal;
|
||||
backButton.onClick.AddListener(async () =>
|
||||
{
|
||||
if (defaultLayer.activeSelf)
|
||||
{
|
||||
await SceneManager.LoadSceneAsync("CreatorLayer");
|
||||
}
|
||||
else
|
||||
{
|
||||
LeaveLevel();
|
||||
}
|
||||
});
|
||||
defaultCreateButton.onClick.AddListener(() =>
|
||||
{
|
||||
JArray currentLevels = BazookaManager.Instance.GetCreatedLevels();
|
||||
Level newLevel = new(Guid.NewGuid().ToString(), $"Unnamed {currentLevels.Count + 1}", "", false, false, -1, new JArray(0, 0), 35f);
|
||||
currentLevels.Add(JObject.FromObject(newLevel));
|
||||
BazookaManager.Instance.SetCreatedLevels(currentLevels);
|
||||
LoadLevel(newLevel.UUID, currentLevels);
|
||||
});
|
||||
levelNameInput.onValueChanged.AddListener(newValue => TitleChangeEvent(newValue));
|
||||
levelDescriptionInput.onValueChanged.AddListener(newValue => DescriptionChangeEvent(newValue));
|
||||
levelEditLevelButton.onClick.AddListener(LaunchEditor);
|
||||
levelDeleteButton.onClick.AddListener(() =>
|
||||
{
|
||||
JArray currentLevels = BazookaManager.Instance.GetCreatedLevels();
|
||||
var item = currentLevels.FirstOrDefault(x => x["uuid"]?.ToString() == levelUuid);
|
||||
item?.Remove();
|
||||
BazookaManager.Instance.SetCreatedLevels(currentLevels);
|
||||
LeaveLevel();
|
||||
});
|
||||
// if (LevelDataManager.Instance.targetSelectionUuid != null)
|
||||
// {
|
||||
// LoadLevel(LevelDataManager.Instance.targetSelectionUuid);
|
||||
// LevelDataManager.Instance.targetSelectionUuid = null;
|
||||
// }
|
||||
}
|
||||
|
||||
void LoadLevel(string targetUuid, JArray currentLevels)
|
||||
{
|
||||
List<Level> currentLevelsList = currentLevels.ToObject<List<Level>>();
|
||||
foreach (var level in currentLevelsList)
|
||||
{
|
||||
if (level.UUID == targetUuid)
|
||||
{
|
||||
levelUuid = targetUuid;
|
||||
if (level.Name != null)
|
||||
{
|
||||
levelNameInput.text = level.Name.Trim()[..Math.Min(16, level.Name.Trim().Length)];
|
||||
}
|
||||
if (level.Description != null)
|
||||
{
|
||||
levelDescriptionInput.text = level.Description.Trim()[..Math.Min(192, level.Description.Trim().Length)];
|
||||
}
|
||||
levelNameInput.interactable = !level.Uploaded;
|
||||
defaultLayer.SetActive(false);
|
||||
levelLayer.SetActive(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void LoadLevel(string targetUuid)
|
||||
{
|
||||
JArray currentLevels = BazookaManager.Instance.GetCreatedLevels();
|
||||
LoadLevel(targetUuid, currentLevels);
|
||||
}
|
||||
|
||||
void LeaveLevel()
|
||||
{
|
||||
levelUuid = null;
|
||||
ResetLevelView();
|
||||
defaultCreateButton.transform.localScale = UnityEngine.Vector3.one;
|
||||
defaultLayer.SetActive(true);
|
||||
levelLayer.SetActive(false);
|
||||
levelNameInput.text = "";
|
||||
levelDescriptionInput.text = "";
|
||||
levelNameInput.interactable = true;
|
||||
}
|
||||
|
||||
void LaunchEditor()
|
||||
{
|
||||
// LevelDataManager.Instance.targetEditorUuid = levelUuid;
|
||||
SceneManager.LoadScene("LevelEditor");
|
||||
}
|
||||
|
||||
void TitleChangeEvent(string newValue)
|
||||
{
|
||||
JArray currentLevels = BazookaManager.Instance.GetCreatedLevels();
|
||||
List<Level> currentLevelsList = currentLevels.ToObject<List<Level>>();
|
||||
foreach (var level in currentLevelsList)
|
||||
{
|
||||
if (level.UUID == levelUuid)
|
||||
{
|
||||
if (newValue.Trim() != "")
|
||||
{
|
||||
level.Name = newValue.Trim()[..Math.Min(16, newValue.Trim().Length)];
|
||||
}
|
||||
}
|
||||
}
|
||||
BazookaManager.Instance.SetCreatedLevels(JArray.FromObject(currentLevelsList));
|
||||
}
|
||||
|
||||
void DescriptionChangeEvent(string newValue)
|
||||
{
|
||||
JArray currentLevels = BazookaManager.Instance.GetCreatedLevels();
|
||||
List<Level> currentLevelsList = currentLevels.ToObject<List<Level>>();
|
||||
foreach (var level in currentLevelsList)
|
||||
{
|
||||
if (level.UUID == levelUuid)
|
||||
{
|
||||
level.Description = newValue.Trim()[..Math.Min(192, newValue.Trim().Length)];
|
||||
}
|
||||
}
|
||||
BazookaManager.Instance.SetCreatedLevels(JArray.FromObject(currentLevelsList));
|
||||
}
|
||||
|
||||
void ResetLevelView()
|
||||
{
|
||||
foreach (Transform child in defaultScrollContent.transform)
|
||||
{
|
||||
GameObject gameObject = child.gameObject;
|
||||
if (gameObject.activeSelf)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
defaultScrollContent.transform.localPosition = new(defaultScrollContent.transform.localPosition.x, 0, defaultScrollContent.transform.localPosition.z);
|
||||
JArray currentLevels = BazookaManager.Instance.GetCreatedLevels();
|
||||
List<Level> currentLevelsList = currentLevels.ToObject<List<Level>>();
|
||||
foreach (var level in currentLevelsList)
|
||||
{
|
||||
var newObj = Instantiate(defaultSampleLevel, defaultSampleLevel.transform.parent, false);
|
||||
TMP_Text child0 = newObj.transform.GetChild(0).GetComponent<TMP_Text>();
|
||||
child0.text = level.Name.Trim()[..Math.Min(16, level.Name.Trim().Length)];
|
||||
newObj.transform.GetChild(1).GetComponent<TMP_Text>().text = level.Verified ? "Verified" : "Unverified";
|
||||
newObj.transform.GetChild(2).GetComponent<Button>().onClick.AddListener(() => LoadLevel(level.UUID));
|
||||
newObj.SetActive(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
8
Assets/Scripts/Types.meta
Normal file
8
Assets/Scripts/Types.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a7419c100995e4ec98c12958d9baa25b
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
42
Assets/Scripts/Types/Level.cs
Normal file
42
Assets/Scripts/Types/Level.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using System.Numerics;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
public class Level
|
||||
{
|
||||
[JsonProperty("uuid")]
|
||||
public string UUID { get; set; }
|
||||
|
||||
[JsonProperty("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
[JsonProperty("description")]
|
||||
public string Description { get; set; }
|
||||
|
||||
[JsonProperty("verified")]
|
||||
public bool Verified { get; set; }
|
||||
|
||||
[JsonProperty("uploaded")]
|
||||
public bool Uploaded { get; set; }
|
||||
|
||||
[JsonProperty("uploadedId")]
|
||||
public BigInteger UploadedID { get; set; }
|
||||
|
||||
[JsonProperty("editorCameraPos")]
|
||||
public JArray EditorCameraPos { get; set; }
|
||||
|
||||
[JsonProperty("editorCameraZoom")]
|
||||
public float EditorCameraZoom { get; set; }
|
||||
|
||||
public Level(string uuid, string name, string description, bool verified, bool uploaded, BigInteger uploadedId, JArray editorCameraPos, float editorCameraZoom)
|
||||
{
|
||||
UUID = uuid;
|
||||
Name = name;
|
||||
Description = description;
|
||||
Verified = verified;
|
||||
Uploaded = uploaded;
|
||||
UploadedID = uploadedId;
|
||||
EditorCameraPos = editorCameraPos;
|
||||
EditorCameraZoom = editorCameraZoom;
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Types/Level.cs.meta
Normal file
2
Assets/Scripts/Types/Level.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 203847ff3940241819e3f0b0cdf114f3
|
||||
Reference in New Issue
Block a user