public static UdonSharpBehaviour AddUdonSharpComponent(this GameObject gameObject, System.Type type) { if (type == typeof(UdonSharpBehaviour)) { throw new System.ArgumentException("Cannot add components of type 'UdonSharpBehaviour', you can only add subclasses of this type"); } if (!typeof(UdonSharpBehaviour).IsAssignableFrom(type)) { throw new System.ArgumentException("Type for AddUdonSharpComponent must be a subclass of UdonSharpBehaviour"); } UdonBehaviour udonBehaviour = gameObject.AddComponent <UdonBehaviour>(); UdonSharpProgramAsset programAsset = UdonSharpProgramAsset.GetProgramAssetForClass(type); udonBehaviour.programSource = programAsset; udonBehaviour.AllowCollisionOwnershipTransfer = false; SerializedObject componentAsset = new SerializedObject(udonBehaviour); SerializedProperty serializedProgramAssetProperty = componentAsset.FindProperty("serializedProgramAsset"); serializedProgramAssetProperty.objectReferenceValue = programAsset.SerializedProgramAsset; componentAsset.ApplyModifiedPropertiesWithoutUndo(); UdonSharpBehaviour proxyComponent = UdonSharpEditorUtility.GetProxyBehaviour(udonBehaviour); if (EditorApplication.isPlaying) { udonBehaviour.InitializeUdonContent(); } return(proxyComponent); }
public static UdonSharpBehaviour AddUdonSharpComponent(this GameObject gameObject, System.Type type) { if (type == typeof(UdonSharpBehaviour)) { throw new System.ArgumentException("Cannot add components of type 'UdonSharpBehaviour', you can only add subclasses of this type"); } if (!typeof(UdonSharpBehaviour).IsAssignableFrom(type)) { throw new System.ArgumentException("Type for AddUdonSharpComponent must be a subclass of UdonSharpBehaviour"); } UdonBehaviour udonBehaviour = gameObject.AddComponent <UdonBehaviour>(); UdonSharpProgramAsset programAsset = UdonSharpProgramAsset.GetProgramAssetForClass(type); udonBehaviour.programSource = programAsset; #pragma warning disable CS0618 // Type or member is obsolete udonBehaviour.SynchronizePosition = false; udonBehaviour.AllowCollisionOwnershipTransfer = false; #pragma warning restore CS0618 // Type or member is obsolete switch (programAsset.behaviourSyncMode) { case BehaviourSyncMode.Continuous: udonBehaviour.SyncMethod = Networking.SyncType.Continuous; break; case BehaviourSyncMode.Manual: udonBehaviour.SyncMethod = Networking.SyncType.Manual; break; case BehaviourSyncMode.None: udonBehaviour.SyncMethod = Networking.SyncType.None; break; } SerializedObject componentAsset = new SerializedObject(udonBehaviour); SerializedProperty serializedProgramAssetProperty = componentAsset.FindProperty("serializedProgramAsset"); serializedProgramAssetProperty.objectReferenceValue = programAsset.SerializedProgramAsset; componentAsset.ApplyModifiedPropertiesWithoutUndo(); UdonSharpBehaviour proxyComponent = UdonSharpEditorUtility.GetProxyBehaviour(udonBehaviour); if (EditorApplication.isPlaying) { udonBehaviour.InitializeUdonContent(); } return(proxyComponent); }
public static UdonSharpBehaviour AddComponent(GameObject gameObject, System.Type type) { if (type == typeof(UdonSharpBehaviour)) { throw new System.ArgumentException("Cannot add components of type 'UdonSharpBehaviour', you can only add subclasses of this type"); } if (!typeof(UdonSharpBehaviour).IsAssignableFrom(type)) { throw new System.ArgumentException("Type for AddUdonSharpComponent must be a subclass of UdonSharpBehaviour"); } UdonBehaviour udonBehaviour = Undo.AddComponent <UdonBehaviour>(gameObject); UdonSharpProgramAsset programAsset = UdonSharpProgramAsset.GetProgramAssetForClass(type); udonBehaviour.programSource = programAsset; #pragma warning disable CS0618 // Type or member is obsolete udonBehaviour.AllowCollisionOwnershipTransfer = false; #pragma warning restore CS0618 // Type or member is obsolete SerializedObject componentAsset = new SerializedObject(udonBehaviour); SerializedProperty serializedProgramAssetProperty = componentAsset.FindProperty("serializedProgramAsset"); serializedProgramAssetProperty.objectReferenceValue = programAsset.SerializedProgramAsset; componentAsset.ApplyModifiedProperties(); System.Type scriptType = programAsset.GetClass(); UdonSharpBehaviour proxyComponent = (UdonSharpBehaviour)Undo.AddComponent(udonBehaviour.gameObject, scriptType); proxyComponent.hideFlags = HideFlags.DontSaveInBuild | #if !UDONSHARP_DEBUG HideFlags.HideInInspector | #endif HideFlags.DontSaveInEditor; proxyComponent.enabled = false; UdonSharpEditorUtility.SetBackingUdonBehaviour(proxyComponent, udonBehaviour); UdonSharpEditorUtility.CopyUdonToProxy(proxyComponent, ProxySerializationPolicy.AllWithCreateUndo); if (EditorApplication.isPlaying) { udonBehaviour.InitializeUdonContent(); } return(proxyComponent); }