skin
This commit is contained in:
24
Assets/Script/CharacterSkin/ChangeCharacterOnSelection.cs
Normal file
24
Assets/Script/CharacterSkin/ChangeCharacterOnSelection.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Assets.HeroEditor4D.Common.Scripts.CharacterScripts;
|
||||
using UnityEngine;
|
||||
|
||||
public class ChangeCharacterOnSelection : MonoBehaviour
|
||||
{
|
||||
public Character4D character;
|
||||
void Start(){
|
||||
CharacterSelection.OnCharacterChanged += RefreshCharacter;
|
||||
RefreshCharacter();
|
||||
}
|
||||
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
CharacterSelection.OnCharacterChanged -= RefreshCharacter;
|
||||
}
|
||||
|
||||
void RefreshCharacter(){
|
||||
if(CharacterSelection.selectedCharJson.Length <= 0){return;}
|
||||
character.FromJson(CharacterSelection.selectedCharJson, true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1a01bf626125f48b38c14bb319871b4b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
89
Assets/Script/CharacterSkin/CharacterSelection.cs
Normal file
89
Assets/Script/CharacterSkin/CharacterSelection.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Firebase.Extensions;
|
||||
using Firebase.Firestore;
|
||||
using Unity.VisualScripting;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class CharacterSelection : MonoBehaviour
|
||||
{
|
||||
|
||||
public static string selectedCharJson;
|
||||
public List<string> predefinedCharacterJsons;
|
||||
public List<Button> predefinedCharacterButtons;
|
||||
|
||||
public static Action OnCharacterChanged;
|
||||
|
||||
|
||||
void Start()
|
||||
{
|
||||
for(int i=0; i < predefinedCharacterButtons.Count; i++){
|
||||
int tempI = i;
|
||||
predefinedCharacterButtons[i].onClick.AddListener(()=>{OnPredefinedCharButtonPress(tempI);});
|
||||
}
|
||||
LoadCharacterSkinCloud();
|
||||
}
|
||||
|
||||
public void LoadCharacterFromString(string characterData){
|
||||
selectedCharJson = characterData;
|
||||
OnCharacterChanged.Invoke();
|
||||
}
|
||||
|
||||
void OnPredefinedCharButtonPress(int i){
|
||||
selectedCharJson = predefinedCharacterJsons[i];
|
||||
OnCharacterChanged.Invoke();
|
||||
SaveCharacterSkinCloud();
|
||||
}
|
||||
|
||||
public static void SaveCharacterSkinCloud(){
|
||||
#if UNITY_EDITOR
|
||||
PlayerPrefs.SetString("skinData", selectedCharJson);
|
||||
PlayerPrefs.Save();
|
||||
#endif
|
||||
|
||||
FirebaseFirestore db = FirebaseFirestore.DefaultInstance;
|
||||
|
||||
Dictionary<string,object> skindataDictionary = new Dictionary<string, object>();
|
||||
skindataDictionary.Add("json", selectedCharJson);
|
||||
|
||||
DocumentReference docRef = db.Collection("SkinData").Document(gplayAuth.userID);
|
||||
docRef.SetAsync(skindataDictionary).ContinueWithOnMainThread(task => {
|
||||
if(task.IsCompleted){
|
||||
Debug.Log("**** Save Completed Firestore ****");
|
||||
}
|
||||
else{
|
||||
Debug.Log("**** Failed to save data to firestore ****");
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
void LoadCharacterSkinCloud(){
|
||||
#if UNITY_EDITOR
|
||||
if(PlayerPrefs.HasKey("skinData")){
|
||||
LoadCharacterFromString(PlayerPrefs.GetString("skinData"));
|
||||
}
|
||||
#endif
|
||||
|
||||
FirebaseFirestore db = FirebaseFirestore.DefaultInstance;
|
||||
DocumentReference docRef = db.Collection("SkinData").Document(gplayAuth.userID);
|
||||
|
||||
docRef.GetSnapshotAsync().ContinueWithOnMainThread(task => {
|
||||
DocumentSnapshot snapshot = task.Result;
|
||||
if(snapshot.Exists){
|
||||
|
||||
LoadCharacterFromString(snapshot.GetValue<string>("json"));
|
||||
|
||||
}else{
|
||||
//show error previous data doesnt exists to load
|
||||
Debug.Log("**** No previous data to load ****");
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
11
Assets/Script/CharacterSkin/CharacterSelection.cs.meta
Normal file
11
Assets/Script/CharacterSkin/CharacterSelection.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f46cf614fdb6e410695e435f02bfcd44
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user