I want to press one of the 4 buttons for switching Characters, and Sync “public static int AvatarNum;” value for control, but I can’t trigger “protected override void OnRealtimeModelReplaced()” in SyncInt Script , I tried a month but didn’t working, please help.
using Normal.Realtime;
using Normal.Realtime.Serialization;
[RealtimeModel]
public partial class SyncBoolModel {
[RealtimeProperty(1, true, true)] private bool _mutenow;
[RealtimeProperty(2, true, true)] private int _avanum;
[RealtimeProperty(3, true, true)] private bool _prince;
[RealtimeProperty(4, true, true)] private bool _pilot;
[RealtimeProperty(5, true, true)] private bool _rose;
[RealtimeProperty(6, true, true)] private bool _fox;
}
/* ----- Begin Normal Autogenerated Code ----- */
public partial class SyncBoolModel : RealtimeModel {
public bool mutenow {
get {
return _mutenowProperty.value;
}
set {
if (_mutenowProperty.value == value) return;
_mutenowProperty.value = value;
InvalidateReliableLength();
FireMutenowDidChange(value);
}
}
public int avanum {
get {
return _avanumProperty.value;
}
set {
if (_avanumProperty.value == value) return;
_avanumProperty.value = value;
InvalidateReliableLength();
FireAvanumDidChange(value);
}
}
public bool prince {
get {
return _princeProperty.value;
}
set {
if (_princeProperty.value == value) return;
_princeProperty.value = value;
InvalidateReliableLength();
FirePrinceDidChange(value);
}
}
public bool pilot {
get {
return _pilotProperty.value;
}
set {
if (_pilotProperty.value == value) return;
_pilotProperty.value = value;
InvalidateReliableLength();
FirePilotDidChange(value);
}
}
public bool rose {
get {
return _roseProperty.value;
}
set {
if (_roseProperty.value == value) return;
_roseProperty.value = value;
InvalidateReliableLength();
FireRoseDidChange(value);
}
}
public bool fox {
get {
return _foxProperty.value;
}
set {
if (_foxProperty.value == value) return;
_foxProperty.value = value;
InvalidateReliableLength();
FireFoxDidChange(value);
}
}
public delegate void PropertyChangedHandler<in T>(SyncBoolModel model, T value);
public event PropertyChangedHandler<bool> mutenowDidChange;
public event PropertyChangedHandler<int> avanumDidChange;
public event PropertyChangedHandler<bool> princeDidChange;
public event PropertyChangedHandler<bool> pilotDidChange;
public event PropertyChangedHandler<bool> roseDidChange;
public event PropertyChangedHandler<bool> foxDidChange;
public enum PropertyID : uint {
Mutenow = 1,
Avanum = 2,
Prince = 3,
Pilot = 4,
Rose = 5,
Fox = 6,
}
#region Properties
private ReliableProperty<bool> _mutenowProperty;
private ReliableProperty<int> _avanumProperty;
private ReliableProperty<bool> _princeProperty;
private ReliableProperty<bool> _pilotProperty;
private ReliableProperty<bool> _roseProperty;
private ReliableProperty<bool> _foxProperty;
#endregion
public SyncBoolModel() : base(null) {
_mutenowProperty = new ReliableProperty<bool>(1, _mutenow);
_avanumProperty = new ReliableProperty<int>(2, _avanum);
_princeProperty = new ReliableProperty<bool>(3, _prince);
_pilotProperty = new ReliableProperty<bool>(4, _pilot);
_roseProperty = new ReliableProperty<bool>(5, _rose);
_foxProperty = new ReliableProperty<bool>(6, _fox);
}
protected override void OnParentReplaced(RealtimeModel previousParent, RealtimeModel currentParent) {
_mutenowProperty.UnsubscribeCallback();
_avanumProperty.UnsubscribeCallback();
_princeProperty.UnsubscribeCallback();
_pilotProperty.UnsubscribeCallback();
_roseProperty.UnsubscribeCallback();
_foxProperty.UnsubscribeCallback();
}
private void FireMutenowDidChange(bool value) {
try {
mutenowDidChange?.Invoke(this, value);
} catch (System.Exception exception) {
UnityEngine.Debug.LogException(exception);
}
}
private void FireAvanumDidChange(int value) {
try {
avanumDidChange?.Invoke(this, value);
} catch (System.Exception exception) {
UnityEngine.Debug.LogException(exception);
}
}
private void FirePrinceDidChange(bool value) {
try {
princeDidChange?.Invoke(this, value);
} catch (System.Exception exception) {
UnityEngine.Debug.LogException(exception);
}
}
private void FirePilotDidChange(bool value) {
try {
pilotDidChange?.Invoke(this, value);
} catch (System.Exception exception) {
UnityEngine.Debug.LogException(exception);
}
}
private void FireRoseDidChange(bool value) {
try {
roseDidChange?.Invoke(this, value);
} catch (System.Exception exception) {
UnityEngine.Debug.LogException(exception);
}
}
private void FireFoxDidChange(bool value) {
try {
foxDidChange?.Invoke(this, value);
} catch (System.Exception exception) {
UnityEngine.Debug.LogException(exception);
}
}
protected override int WriteLength(StreamContext context) {
var length = 0;
length += _mutenowProperty.WriteLength(context);
length += _avanumProperty.WriteLength(context);
length += _princeProperty.WriteLength(context);
length += _pilotProperty.WriteLength(context);
length += _roseProperty.WriteLength(context);
length += _foxProperty.WriteLength(context);
return length;
}
protected override void Write(WriteStream stream, StreamContext context) {
var writes = false;
writes |= _mutenowProperty.Write(stream, context);
writes |= _avanumProperty.Write(stream, context);
writes |= _princeProperty.Write(stream, context);
writes |= _pilotProperty.Write(stream, context);
writes |= _roseProperty.Write(stream, context);
writes |= _foxProperty.Write(stream, context);
if (writes) InvalidateContextLength(context);
}
protected override void Read(ReadStream stream, StreamContext context) {
var anyPropertiesChanged = false;
while (stream.ReadNextPropertyID(out uint propertyID)) {
var changed = false;
switch (propertyID) {
case (uint) PropertyID.Mutenow: {
changed = _mutenowProperty.Read(stream, context);
if (changed) FireMutenowDidChange(mutenow);
break;
}
case (uint) PropertyID.Avanum: {
changed = _avanumProperty.Read(stream, context);
if (changed) FireAvanumDidChange(avanum);
break;
}
case (uint) PropertyID.Prince: {
changed = _princeProperty.Read(stream, context);
if (changed) FirePrinceDidChange(prince);
break;
}
case (uint) PropertyID.Pilot: {
changed = _pilotProperty.Read(stream, context);
if (changed) FirePilotDidChange(pilot);
break;
}
case (uint) PropertyID.Rose: {
changed = _roseProperty.Read(stream, context);
if (changed) FireRoseDidChange(rose);
break;
}
case (uint) PropertyID.Fox: {
changed = _foxProperty.Read(stream, context);
if (changed) FireFoxDidChange(fox);
break;
}
default: {
stream.SkipProperty();
break;
}
}
anyPropertiesChanged |= changed;
}
if (anyPropertiesChanged) {
UpdateBackingFields();
}
}
private void UpdateBackingFields() {
_mutenow = mutenow;
_avanum = avanum;
_prince = prince;
_pilot = pilot;
_rose = rose;
_fox = fox;
}
}
/* ----- End Normal Autogenerated Code ----- */
public class Scenechange : MonoBehaviour
{
public static int AvatarNum;
public void changechar()
{
AvatarNum = 0;
hosting.gameObject.SetActive(false);
charset.gameObject.SetActive(true);
}
//char select
public void piloter()
{
AvatarNum =2;
Changing();
getback();
}
public void foxer()
{
AvatarNum =4;
Changing();
getback();
}
public void roser()
{
AvatarNum =3;
Changing();
getback();
}
public void princer()
{
AvatarNum =1;
Changing();
getback();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Normal.Realtime;
using Midlane;
public class SyncInt : RealtimeComponent<SyncBoolModel>
{
[SerializeField] private GameObject prince;
[SerializeField] private GameObject pilot;
[SerializeField] private GameObject rose;
[SerializeField] private GameObject fox;
private int avatarNum;
private void Awake()
{
avatarNum = Scenechange.AvatarNum;
}
protected override void OnRealtimeModelReplaced(SyncBoolModel previousModel, SyncBoolModel currentModel)
{
if (previousModel != null)
{
previousModel.avanumDidChange -= AvanumDidChange;
Debug.Log("1 Active " + previousModel.avanum);
}
if (currentModel != null)
{
if (currentModel.isFreshModel)
currentModel.avanum = avatarNum;
Updateavatarsec(currentModel);
currentModel.avanumDidChange += AvanumDidChange;
Debug.Log("2 Active " + currentModel.avanum);
}
}
private void AvanumDidChange(SyncBoolModel model, int value)
{
Updateavatarsec(model);
}
private void Updateavatarsec(SyncBoolModel model)
{
avatarNum = model.avanum;
switch (model.avanum)
{
case 0:
pilot.SetActive(false);
fox.SetActive(false);
rose.SetActive(false);
prince.SetActive(false);
break;
case 1:
prince.SetActive(true);
break;
case 2:
pilot.SetActive(true);
break;
case 3:
rose.SetActive(true);
break;
case 4:
fox.SetActive(true);
break;
}
}
}