MixedRealityToolkit-Unity icon indicating copy to clipboard operation
MixedRealityToolkit-Unity copied to clipboard

SyncObject.ObjectChanged not firing

Open kyleWillson opened this issue 8 years ago • 4 comments

Overview

I am trying to create a SyncModel representing a GameObject (SyncGameObject), so it has the following fields:

  • GameObject myObject; // This is the Object being represented
  • SyncTransform myTransform; // The Transform used to sync interactions
  • SyncBool isActive; // If the gameObject is active or not
  • ChangeTransform(Transform) // Gets called through an interaction script on the GameObject to set the new values to be synced.
  • OnTransformChanged(SyncObject) // myTransform.ObjectChanged += OnTransformChanged DOES NOT FIRE... why? However SyncTransform.OnPositionChanged gets fired. But I dont know how to use that data, because the GameObject Reference goes null once I leave the set function. Why?

So if a parent container class (SyncGameObject) subscribes to any SyncObject.ObjectChanged events, they don't fire. And I have been killing myself to figure out how to do that.

Expected Behavior

SyncGameObject.OnTransformChanged() gets fired

Actual Behavior

SyncGameObject.OnTransformChanged() doesn't get fired

Steps to reproduce

TestSpawnManager - MonoBehaviour Script placed on prefab to start initialization

`public class TestSpawnedManager : MonoBehaviour {

// Use this for initialization
void Start () {
    Debug.LogFormat("TestSpawnedManager.Start(): Initiallizing the sync model...");

    ((TestSyncSpawnManager)GetComponent<DefaultSyncModelAccessor>().SyncModel).InitializeExternally(this);
}

// Update is called once per frame
void Update () {
	
}

}`

TestSyncSpawnManager - SyncSpawnedObject Data Model

`[SyncDataClass] public class TestSyncSpawnManager : SyncSpawnedObject { [SyncData] public SyncArray InteractiveChildren = new SyncArray("Interactive Children");

public TestSpawnedManager manager;

public override void Initialize(string name, string parentPath)
{
    base.Initialize(name, parentPath);
}

public void InitializeExternally(TestSpawnedManager _myManager)
{
    manager = _myManager;

    Debug.LogFormat("TestSyncSpawnManager.InitializeExternally(): GameObject: {0}", manager.name);

    // Initialize any interactive GameObjects
    InitializeChildren();
}

public void InitializeChildren()
{
    Debug.LogFormat("TestSyncSpawnManager.InitializeChildren(): Gathering and making SyncGameObjects of the children");

    // Get and Loop through the children
    Transform[] children = manager.GetComponentsInChildren<Transform>(true);
    for (int i = 1; i < children.Length; i++)
    {
        GameObject childObj = children[i].gameObject;

        GestureInteract gestInt = childObj.GetComponent<GestureInteract>();
        if (gestInt != null)
        {
            TestSyncGameObject childModel = InteractiveChildren.AddObject(new TestSyncGameObject(childObj.name + "_SyncModel", childObj));
            
            Debug.LogFormat("TestSyncSpawnManager.InitializeChildren(): GameObject: {0}. ", childObj.name);
        }
    }
}

} `

TestSyncGameObject - SyncModel representing a gameobject with an Interact script on it

`[SyncDataClass] public class TestSyncGameObject : SyncObject { [SyncData] public SyncTransform MySyncTransform = new SyncTransform("Transform");

public GameObject gameObject { get; set; }
public Transform transform { get; set; }

public TestSyncGameObject()
{
    Debug.LogFormat("TestSyncGameObject.SyncGameObject(): EMPTY CONSTRUCTOR");
}

public TestSyncGameObject(string _field, GameObject _object) : base(_field)
{
    gameObject = _object;
    transform = gameObject.transform;

    Debug.LogFormat("TestSyncGameObject.TestSyncGameObject(): Initializing SyncGameObject for: {0}", gameObject.name);

    MySyncTransform.Position.Value = transform.localPosition;
    MySyncTransform.Rotation.Value = transform.localRotation;
    MySyncTransform.Scale.Value = transform.localScale;

    // Check if there is a Gesture Interact for the GameObejct
    GestureInteract gestInteract = gameObject.GetComponent<GestureInteract>();
    if (gestInteract != null)
    {
        // Give the Sync Field name as the ID for any GestureInteracts
        GestureInteract gestInt = gameObject.GetComponent<GestureInteract>();
        if (gestInt != null)
        {
            Debug.LogFormat("SyncGameObject.InitializeExternally(): Initializing GestureInteract");
            gestInt.InitializeSharing(this);
        }
        
    }

    MySyncTransform.ObjectChanged += OnSyncTransformObjectChanged;
    MySyncTransform.PositionChanged += OnSyncTransformPositionFlag;
    MySyncTransform.Position.ObjectChanged += OnSyncTransformPositionObjectChanged;

}


public void ChangeTransformHandler(Transform _newTransform)
{
    Debug.LogFormat("TestSyncGameObject.ChangeTransformHandler(): {0}'s new position: {1}", _newTransform.name, _newTransform.localPosition);

    MySyncTransform.Position.Value = _newTransform.localPosition;
    MySyncTransform.Rotation.Value = _newTransform.localRotation;
    MySyncTransform.Scale.Value = _newTransform.localScale;
           
}

private void OnSyncTransformPositionObjectChanged(SyncObject obj)
{
    Debug.LogFormat("TestSyncGameObject.OnSyncTransformPositionObjectChanged(): SyncTransform.Position Object Changed");  // DOES NOT FIRE
}

private void OnSyncTransformPositionFlag()
{
    Debug.LogFormat("TestSyncGameObject.OnSyncTransformPositionFlag(): SyncTransform.PositionChanged Action Flag");  // DOES NOT FIRE
}

private void OnSyncTransformObjectChanged(SyncObject obj)
{
    Debug.LogFormat("TestSyncGameObject.OnSyncTransformObjectChanged(): SyncTransform Object Changed");  // DOES NOT FIRE
}

}`

** SyncTransform - Added Print statements**

` [SyncDataClass] public class SyncTransform : SyncObject { [SyncData] public SyncVector3 Position = new SyncVector3("Position"); [SyncData] public SyncQuaternion Rotation = new SyncQuaternion("Rotation"); [SyncData] public SyncVector3 Scale = new SyncVector3("Scale");

    public event Action PositionChanged;
    public event Action RotationChanged;
    public event Action ScaleChanged;

    public SyncTransform(string field)
        : base(field)
    {
        Position.ObjectChanged += OnPositionChanged;
        Rotation.ObjectChanged += OnRotationChanged;
        Scale.ObjectChanged += OnScaleChanged;
    }

    private void OnPositionChanged(SyncObject obj)
    {
        Debug.LogFormat("SyncTransform.OnPositionChanged(): CHANGE ========="); // FIRES
        PositionChanged.RaiseEvent();
    }

    private void OnRotationChanged(SyncObject obj)
    {
        Debug.LogFormat("SyncTransform.OnRotationChanged(): CHANGE =========");  // FIRES
        RotationChanged.RaiseEvent();
    }

    private void OnScaleChanged(SyncObject obj)
    {
        Debug.LogFormat("SyncTransform.OnScaleChanged(): CHANGE =========");  // FIRES
        ScaleChanged.RaiseEvent();
    }
}

}`

Unity Editor Version

2017.3.1

Mixed Reality Toolkit Release Version

2017.2.1

kyleWillson avatar Feb 20 '18 21:02 kyleWillson

No repro steps?

StephenHodgson avatar Feb 20 '18 22:02 StephenHodgson

@StephenHodgson is this good enough or do you need more?

kyleWillson avatar Feb 21 '18 14:02 kyleWillson

Thanks for the update

StephenHodgson avatar Feb 23 '18 17:02 StephenHodgson

Closing issues older than 180 days. If this is still an issue, please reactivate with recent information.

wiwei avatar Apr 11 '19 20:04 wiwei