How to Synchronize PhotonAnimatorView for dynamically added animator and animation controller?

Options
Hi
I add the animator and animation controller for the animator dynamically in the game and it is done for size optimization that way.
Now I am converting my existing games to multiplayer setup.I have added a code that should have synchronized over the network but I dont see the animation controller for the other player added.
                   Photon.Pun.PhotonView photonView = _animator.gameObject.GetComponent<Photon.Pun.PhotonView>();
                    photonView.ObservedComponents = new System.Collections.Generic.List<Component>();
                    photonView.Synchronization = Photon.Pun.ViewSynchronization.UnreliableOnChange;
                    photonView.observableSearch = Photon.Pun.PhotonView.ObservableSearch.AutoFindActive;
                    Photon.Pun.PhotonAnimatorView photonAnimatorView = _animator.gameObject.AddComponent<Photon.Pun.PhotonAnimatorView>();


                    for (int count = 0; count < _animator.layerCount; count++) {
                        photonAnimatorView.SetLayerSynchronized(count, Photon.Pun.PhotonAnimatorView.SynchronizeType.Discrete);
                    }

                    foreach(var animatorControllerParameter in _animator.parameters)
                    {
                        photonAnimatorView.SetParameterSynchronized(animatorControllerParameter.name,
                            (Photon.Pun.PhotonAnimatorView.ParameterType)animatorControllerParameter.type, Photon.Pun.PhotonAnimatorView.SynchronizeType.Discrete);
                    }
                    photonView.ObservedComponents.Add(photonAnimatorView);

I dont see the animation controller added for the other synchronized player for the above code, also I see the error "Animator doesnt have layers setup to synchronize" and "Animators doesn't have any parameters setup for synchronize" I see both the for loops for layer and parameter is been executed but I still see the message in the editor more over I dont see the animationcontroller updating.
What am I missing here?

Comments

  • If you're instantiating these things dynamically, are you instantiating them with the Photon Instantiate or the Unity Instantiate?
  • Raghav
    Raghav
    edited September 2020
    Options
    I have a ResourceLoader that is inherited from IPunPrefabPool, which then lets me create the new PrefabPool object and then let me Instantiate the object using PhotonNetwork.Instantiate.
    PhotonNetwork.PrefabPool = new PunResourceLoader();
    var gameobject = PhotonNetwork.Instantiate(modelPath, position, Quaternion.Euler(rotation));
    

    I am using this setup as I am loading the prefab from assetbundle
  • emotitron
    Options
    I am not fully following the problem, but at a glance you are up against how animator syncs have to operate. There are details about the Animator that can only be extracted in the Editor, and can't be extracted at runtime. Trying to add Animators dynamically and synchronize them is going to be a long hard road.
  • Tobias
    Options
    Via mail you added:
    This is done to reduce the size of our AssetBundles as we have many characters for male, female and child and they share their respective animatorcontrollers, to optimise the size and download time for assetbundles we separate the animatorcontrollers from the characters and add it on runtime.

    Due to how the engine and animation sync works, it's best to have the animator on the prefab. As a workaround, maybe you can create a prefab that's flexible to use different skins? This can be done in OnPhotonInstantiate, using the photonView.InstantiationData. This data is set via the last parameter of PhotonNetwork.Instantiate().
  • Raghav
    Options
    Thanks for the response, Will try to follow this and see how we can get this suggestion to work. Will drop the message on how it works for us.

    Thanks again
  • Raghav
    Options
    This is update for my current approach
    We cant move away from dynamically instantiating our Prefab and AnimationControllers, as we have the system which has been working and have been working great in terms of space and size of our assetbundles.

    So to get around with the issue of animationcontroller being part of the prefab, what I do currently is create the skeleton for the avatar with photonAnimatorView and AnimationController before I create an instance of it, I see it is set up properly without any console error, I want to know if this approach is in right direction.
  • Tobias
    Options
    As long as the Editor Inspector code can run once on the prefab, it will be fine.
    Again: The PhotonAnimatorView requires some in-Editor setup to work at runtime.

    You can come up with your own CustomAnimatorView, if that makes your life easier but the provided one, does not work without initial setup in the Editor.
  • emotitron
    Options
    In my experience in writing the animator sync for SimpleNetworkSync, it has to be added before build/runtime. The information scraped out of the Animation Controller requires editor only tools. Names are all reduced to hashes at runtime. You might be able to write your own Animator sync, but it is a bit of a learning curve.

    What you are suggesting sounds like you are still trying to add the PhotonAnimatorView at runtime, and I am pretty sure that will fail. Worth a shot though. I am not sure who wrote that component, but it likely uses the same editor only methods as mine and others. In which case it will fail.