Using UnityEngine
Using UnityEngine
using Firebase;
using Firebase.Database;
using Firebase.Extensions;
using TMPro;
using System.Collections.Generic;
[SerializeField]
private GameObject[] prefabs; // Array of prefabs for loading
void Start()
{
FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
{
var dependencyStatus = task.Result;
if (dependencyStatus == DependencyStatus.Available)
{
FirebaseApp app = FirebaseApp.DefaultInstance;
databaseReference = FirebaseDatabase.DefaultInstance.RootReference;
LoadDataFromFirebase();
}
else
{
Debug.LogError($"Could not resolve all Firebase dependencies:
{dependencyStatus}");
}
});
}
void Update()
{
ctrlKeyHeld = Input.GetKey(KeyCode.LeftControl) ||
Input.GetKey(KeyCode.RightControl);
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 1000))
{
Select(hit.collider.gameObject);
}
}
if (Input.GetMouseButtonDown(1))
{
Deselect();
}
if (Input.GetKeyDown(KeyCode.K))
{
SaveSelectedObject();
}
if (Input.GetKeyDown(KeyCode.L))
{
LoadSelectedObject();
}
}
SaveDataToFirebase();
}
}
if (loadedPrefab != null)
{
GameObject loadedObject = Instantiate(loadedPrefab);
loadedObject.transform.position = lastSavedPosition;
Renderer objRenderer = loadedObject.GetComponent<Renderer>();
if (objRenderer != null)
{
// Update the color of the loaded object
Color color;
if (ColorUtility.TryParseHtmlString("#" +
lastSavedData.Color, out color))
{
objRenderer.material.color = color;
}
else
{
Debug.LogError("Failed to parse color: " +
lastSavedData.Color);
}
}
else
{
Debug.LogError("Renderer not found on loaded object.");
}
Debug.Log("Loaded the last saved object.");
}
else
{
Debug.LogError("Prefab not found at index: " + prefabIndex);
}
}
else
{
Debug.LogError("Invalid prefab index: " + prefabIndex);
}
}
else
{
Debug.Log("No saved items to load.");
}
}
if (loadedPrefab != null)
{
GameObject loadedObject = Instantiate(loadedPrefab);
loadedObject.transform.position = new Vector3(data.X,
data.Y, data.Z);
Renderer objRenderer =
loadedObject.GetComponent<Renderer>();
if (objRenderer != null)
{
// Update the color of the loaded object
Color color;
if (ColorUtility.TryParseHtmlString("#" +
data.Color, out color))
{
objRenderer.material.color = color;
}
else
{
Debug.LogError("Failed to parse color: " +
data.Color);
}
}
else
{
Debug.LogError("Renderer not found on loaded
object.");
}
}
else
{
Debug.LogError("Prefab not found at index: " +
prefabIndex);
}
}
else
{
Debug.LogError("Invalid prefab index: " + prefabIndex);
}
}
Debug.Log("Loaded " + savedItems.Count + " items from the
database.");
}
});
}
if (objectToId.ContainsKey(obj))
{
idTextObj.GetComponent<TextMeshProUGUI>().text = "ID: " +
objectToId[obj];
}
else
{
objectToId[obj] = idCounter;
idTextObj.GetComponent<TextMeshProUGUI>().text = "ID: " + idCounter;
idCounter++;
}
textTmpObj.GetComponent<TextMeshProUGUI>().text = "";
colorTextObj.GetComponent<TextMeshProUGUI>().text = "";
prefabNameTextObj.GetComponent<TextMeshProUGUI>().text = "";
idTextObj.GetComponent<TextMeshProUGUI>().text = "";
}
[System.Serializable]
public class SelectionData
{
public float X;
public float Y;
public float Z;
public string Color;
public int PrefabIndex;
public int Id;
}
}