Skin network sync, new server build

This commit is contained in:
2024-09-26 23:46:18 +05:30
parent c29b03d428
commit e711e19fe1
8 changed files with 102 additions and 98 deletions

View File

@@ -18,6 +18,7 @@ public class ChangeCharacterOnSelection : MonoBehaviour
}
void RefreshCharacter(){
if(CharacterSelection.selectedCharJson == null){return;}
if(CharacterSelection.selectedCharJson.Length <= 0){return;}
character.FromJson(CharacterSelection.selectedCharJson, true);
}

View File

@@ -1,16 +1,14 @@
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 static string selectedCharJson = "";
public List<string> predefinedCharacterJsons;
public List<Button> predefinedCharacterButtons;
@@ -19,30 +17,33 @@ public class CharacterSelection : MonoBehaviour
void Start()
{
for(int i=0; i < predefinedCharacterButtons.Count; i++){
for (int i = 0; i < predefinedCharacterButtons.Count; i++)
{
int tempI = i;
predefinedCharacterButtons[i].onClick.AddListener(()=>{OnPredefinedCharButtonPress(tempI);});
predefinedCharacterButtons[i].onClick.AddListener(() => { OnPredefinedCharButtonPress(tempI); });
}
LoadCharacterSkinCloud();
}
public void LoadCharacterFromString(string characterData){
public void LoadCharacterFromString(string characterData)
{
selectedCharJson = characterData;
OnCharacterChanged.Invoke();
}
void OnPredefinedCharButtonPress(int i){
void OnPredefinedCharButtonPress(int i)
{
selectedCharJson = predefinedCharacterJsons[i];
OnCharacterChanged.Invoke();
SaveCharacterSkinCloud();
}
public static void SaveCharacterSkinCloud(){
#if UNITY_EDITOR
PlayerPrefs.SetString("skinData", selectedCharJson);
PlayerPrefs.Save();
#endif
public static void SaveCharacterSkinCloud()
{
#if UNITY_EDITOR
PlayerPrefs.SetString("skinData", selectedCharJson);
PlayerPrefs.Save();
#else
FirebaseFirestore db = FirebaseFirestore.DefaultInstance;
Dictionary<string,object> skindataDictionary = new Dictionary<string, object>();
@@ -58,16 +59,18 @@ public class CharacterSelection : MonoBehaviour
}
});
#endif
}
void LoadCharacterSkinCloud(){
#if UNITY_EDITOR
if(PlayerPrefs.HasKey("skinData")){
LoadCharacterFromString(PlayerPrefs.GetString("skinData"));
}
#endif
void LoadCharacterSkinCloud()
{
#if UNITY_EDITOR
if (PlayerPrefs.HasKey("skinData"))
{
LoadCharacterFromString(PlayerPrefs.GetString("skinData"));
}
#else
FirebaseFirestore db = FirebaseFirestore.DefaultInstance;
DocumentReference docRef = db.Collection("SkinData").Document(gplayAuth.userID);
@@ -84,6 +87,8 @@ public class CharacterSelection : MonoBehaviour
}
});
#endif
}
}

View File

@@ -14,14 +14,13 @@ using UnityEngine.SocialPlatforms;
using Firebase.Auth;
using UnityEngine.UI;
using Newtonsoft.Json;
using Unity.Properties;
public class playerNetwork : NetworkBehaviour
{
public const float ATTACK_COOLDOWN = 0.6f;
[HideInInspector]
public StatManager statManager;
public CharacterSelection charSelectionManager;
//public const int XPFORLEVEL = 10;
[SyncVar(hook =nameof(OnHealthChanged))] public int health = 100;
public Character4D character;
@@ -44,6 +43,7 @@ public class playerNetwork : NetworkBehaviour
[SyncVar(hook = nameof(OnXpChanged))]
public int XP;
public int lvl2 { get{
return GetLevelForKills2(enemyKillCount);
}}
@@ -124,6 +124,9 @@ public class playerNetwork : NetworkBehaviour
[SyncVar]
public int playerCoin;
[SyncVar]
public string myCharJson;
public TMP_Text txtEnemyKillCount;
public TMP_Text txtPlayerName;
public TMP_Text questText;
@@ -200,8 +203,10 @@ public class playerNetwork : NetworkBehaviour
// }
if(!isLocalPlayer){
canvas.SetActive(false);
if(!isServer){
CmdRequestCharJson();
}
}else{
localPlayerTransform = transform;
@@ -216,12 +221,21 @@ public class playerNetwork : NetworkBehaviour
if(isServer){
playerName = gplayAuth.userNameCloud;
myCharJson = CharacterSelection.selectedCharJson;
RpcBroadcastCharJson(CharacterSelection.selectedCharJson);
}
else{
CmdSetName(gplayAuth.userNameCloud);
CmdSetCharJson(CharacterSelection.selectedCharJson);
}
}
}
void LoadCharFromJson(string json){
if(json.Length <=0){return;}
character.FromJson(json,true);
}
public void SavePlayerData(){
@@ -314,10 +328,28 @@ public class playerNetwork : NetworkBehaviour
}
});
}
[Command]
void CmdSetName(string nameValue){
playerName = nameValue;
}
[Command(requiresAuthority =false)]
void CmdRequestCharJson(){
RpcBroadcastCharJson(myCharJson);
}
[Command]
void CmdSetCharJson(string newValue){
myCharJson = newValue;
RpcBroadcastCharJson(newValue);
LoadCharFromJson(newValue);
}
[ClientRpc]
void RpcBroadcastCharJson(string newValue){
LoadCharFromJson(newValue);
}
void OnDirectionChanged(Vector2 oldVal, Vector2 newVal){
character.SetDirection(newVal);
}