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

Dynamically Created SyncTransform and TransformSynchronizer Doesnt Sync

Open kyleWillson opened this issue 9 years ago • 1 comments

Overview

I am trying to dynamically create and link GameObject Transforms with the SyncModel. The idea is my SyncSpawnModel will go iterate through its GameObject's children looking for a script and when it finds a child with it, it adds a new SyncTransform to the SyncArray. However, when I use the SyncArray no changes get synced up, but if I use a single SyncTransform it works. Which can be seen below with the Script where it is just a single SyncTransform.

Also what are the tags [SyncData] and [SyncDataClass] specifically used for, I see that none of the MRTK SyncModels have [SyncDataClass] above them, what is this for?

I have been trying to get this to dynamically add models to the SyncArray for 4 straight days and have nothing to show for it. So any tips are greatly appreciated!

Expected Behavior

I should be able to dynamically add Transforms to the model using a SyncArray<SyncTransform/SyncGameObject> and all the changes get synced with the network.

Actual Behavior

Does not sync using the SyncArray, but it does when its just a single SyncTransform

Steps to reproduce

MySyncSpawnModel: which gets initialized by a MonoBehavior Script I have on the prefab itself, and calls InitializeExternally().

[SyncDataClass]
public class SyncSpawnTestDynamicInteract : SyncSpawnedObject
{
    public bool LOCAL_DEBUG = true;

    [SyncData] public SyncArray<SyncTestGameObject> GestureInteracts = new SyncArray<SyncTestGameObject>("Gesture Interacts");

    [SyncData] public SyncTransform SingleTransform = new SyncTransform("Single Test Transform");

    public override void Initialize(string name, string parentPath)
    {
        if (LOCAL_DEBUG)
        {
            string toPrint = string.Format("SyncSpawnTestDynamicInteract.Initialize(): ");
            Debug.Log(toPrint);

        }

        base.Initialize(name, parentPath);
    }

    public void InitializeExternally()
    {
        Debug.LogFormat("SyncSpawnTestDynamicInteract.InitializeExternally(): Init Model...");

        DynamicallyCreateTransform SingleDynTranform = GameObject.GetComponentInChildren<DynamicallyCreateTransform>(true);
        if(SingleDynTranform != null)
        {
            SingleTransform.Position.Value = SingleDynTranform.transform.localPosition;
            SingleTransform.Rotation.Value = SingleDynTranform.transform.localRotation;
            SingleTransform.Scale.Value = SingleDynTranform.transform.localScale;

            TransformSynchronizer transSyncr = SingleDynTranform.gameObject.AddComponent<TransformSynchronizer>();


            transSyncr.TransformDataModel = SingleTransform;

            Debug.LogFormat("SyncSpawnTestDynamicInteract.InitializeExternally(): Created Single Dynamic Transform");
        }

        // Find any Children with <DynamicallyCreateTransformArr> and add them to the SyncArray
        DynamicallyCreateTransformArr[] DynTransArr = GameObject.GetComponentsInChildren<DynamicallyCreateTransformArr>(true);

        for (int i = 0; i < DynTransArr.Length; i++)
        {
            SyncTestGameObject testObj = GestureInteracts.AddObject(new SyncTestGameObject("Test GameObject", DynTransArr[i].gameObject));

            Debug.LogFormat("SyncSpawnTestDynamicInteract.InitializeExternally(): Created Dynamic Transform Sync Array");
        }
    }
    
}

My SyncGameObject: None of the OnChange functions get called. And I have NO idea why...

[SyncDataClass]
public class SyncTestGameObject : SyncObject
{
    [SyncData] public SyncTransform transform = new SyncTransform("Test Sync GameObject Transform");

    public GameObject gameObject;

    public SyncTestGameObject(string field, GameObject _obj) : base(field)
    {
        gameObject = _obj;

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

        TransformSynchronizer transSyncr = gameObject.AddComponent<TransformSynchronizer>();


        transSyncr.TransformDataModel = transform;

        Debug.LogFormat("SyncTestGameObject.SyncTestGameObject(): Created SyncObject for {0}, TransSync: {1}", gameObject.name, transSyncr != null);

        transform.ObjectChanged += OnTransformChanged;
        transform.Position.ObjectChanged += OnTransformSyncPositionChanged;
        transform.PositionChanged += OnTransformPositionChanged;
    }

    private void OnTransformPositionChanged()
    {
        Debug.LogFormat("SyncTestGameObject.OnTransformPositionChanged(): Detected change in SyncTransform.PositionChanged");
    }

    private void OnTransformSyncPositionChanged(SyncObject obj)
    {
        Debug.LogFormat("SyncTestGameObject.OnTransformChanged(): Detected change in SyncTransform.Position.ObjectChanged");
    }

    private void OnTransformChanged(SyncObject obj)
    {
        Debug.LogFormat("SyncTestGameObject.OnTransformSyncPositionChanged(): Detected change in Transform.ObjectChanged");

        
        // Apply the new transform changes to the gameObject
    }

    public SyncTestGameObject(string field) : base(field) { }

    public SyncTestGameObject() : base(string.Empty) { }

}

MRTK: SyncTransform: OnPositionChanged gets called. So how do I adjust the GameObject Position from there?

public class SyncTransform : SyncObject
    {
        public bool LOCAL_DEBUG = true;

        [SyncData] public SyncVector3 Position = new SyncVector3("Position");
        [SyncData] public SyncQuaternion Rotation = new SyncQuaternion("Rotation");
        [SyncData] public SyncVector3 Scale = new SyncVector3("Scale");

        [SyncData] public SyncString GameObjectID;

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

        public Transform myTransform;
        public SyncGameObject syncGameObject;

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

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

        private void OnPositionChanged(SyncObject obj)
        {
            if (LOCAL_DEBUG)
            {
                string toPrint = string.Format("SyncTransform.OnPositionChanged(): Change Detected");
                Debug.Log(toPrint);
            }

            PositionChanged.RaiseEvent();
        }

        private void OnRotationChanged(SyncObject obj)
        {
            RotationChanged.RaiseEvent();
        }

        private void OnScaleChanged(SyncObject obj)
        {
            ScaleChanged.RaiseEvent();
        }
    }
}

My Script on the prefab that calls the SpawnModel InitializeExternally

public class SpawnedDynamicGestureInteractManager : MonoBehaviour
{
    // Use this for initialization
    void Start()
    {
        ((SyncSpawnTestDynamicInteract)GetComponent<DefaultSyncModelAccessor>().SyncModel).InitializeExternally();
    }
}

Unity Editor Version

2017.1.2

Mixed Reality Toolkit Release Version

2017.1.2

kyleWillson avatar Nov 16 '17 17:11 kyleWillson

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