Trigger









How do I use Trigger
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: JumpAndRunMovement.cs Copy
52     void UpdateJumping()
53     {
54         if( Input.GetKey( KeyCode.Space ) == true && m_IsGrounded == true )
55         {
56             m_Animator.SetTrigger( "IsJumping" );
57             m_Body.AddForce( Vector2.up * JumpForce );
58             m_PhotonView.RPC( "DoJump", PhotonTargets.Others );
59         }
60     }
File name: JumpAndRunMovement.cs Copy
63     void DoJump()
64     {
65         m_Animator.SetTrigger( "IsJumping" );
66     }
File name: SideWalls.cs Copy
7  void OnTriggerEnter2D(Collider2D hitInfo) {
8   if (hitInfo.name == "Ball")
9   {
10    string wallName = transform.name;
11    GameManager.Score (wallName);
12    hitInfo.gameObject.SendMessage ("RestartGame", 1, SendMessageOptions.RequireReceiver);
13   }
14  }
File name: OnCollideSwitchTeam.cs Copy
9     public void OnTriggerEnter(Collider other)
10     {
11         // it's ridiculously easy to switch teams. you only have to make sure you do it for your own characters
12         // (this trigger is called on all clients, when a user's character enters the trigger...)
13
14         // find a PhotonView and check if the character "isMine". Only then, set this client's player-team.
15         PhotonView otherPv = other.GetComponent();
16         if (otherPv != null && otherPv.isMine)
17         {
18             PhotonNetwork.player.SetTeam(this.TeamToSwitchTo);
19         }
20     }
File name: PickupTriggerForward.cs Copy
14     public void OnTriggerEnter(Collider other)
15     {
16         PickupItem parentPickupItem = this.transform.parent.GetComponent();
17         if (parentPickupItem != null)
18         {
19            parentPickupItem.OnTriggerEnter(other);
20         }
21
22         // by enabling this log, you can see that more triggers are forwarded than necessary.
23         // collisions with any collider will trigger, which is not perfect but OK in the demo.
24         //Debug.Log("Triggered. Called parent: " + ((parentPickupItem != null)));
25     }
File name: PhotonConverter.cs Copy
18     public static void RunConversion()
19     {
20         //Ask if user has made a backup.
21         int option = EditorUtility.DisplayDialogComplex("Conversion", "Attempt automatic conversion from Unity Networking to Photon Unity Networking \"PUN\"?", "Yes", "No!", "Pick Script Folder");
22         switch (option)
23         {
24             case 0:
25                 break;
26             case 1:
27                 return;
28             case 2:
29                 PickFolderAndConvertScripts();
30                 return;
31             default:
32                 return;
33         }
34
35         //REAAAALY?
36         bool result = EditorUtility.DisplayDialog("Conversion", "Disclaimer: The code conversion feature is quite crude, but should do it's job well (see the sourcecode). A backup is therefore strongly recommended!", "Yes, I've made a backup: GO", "Abort");
37         if (!result)
38         {
39             return;
40         }
41         Output(EditorApplication.timeSinceStartup + " Started conversion of Unity networking -> Photon");
42
43         //Ask to save current scene (optional)
44         EditorApplication.SaveCurrentSceneIfUserWantsTo();
45
46         EditorUtility.DisplayProgressBar("Converting..", "Starting.", 0);
47
48         //Convert NetworkViews to PhotonViews in Project prefabs
49         //Ask the user if we can move all prefabs to a resources folder
50         bool movePrefabs = EditorUtility.DisplayDialog("Conversion", "Can all prefabs that use a PhotonView be moved to a Resources/ folder? You need this if you use Network.Instantiate.", "Yes", "No");
51
52
53         string[] prefabs = Directory.GetFiles("Assets/", "*.prefab", SearchOption.AllDirectories);
54         foreach (string prefab in prefabs)
55         {
56             EditorUtility.DisplayProgressBar("Converting..", "Object:" + prefab, 0.6f);
57
58             Object[] objs = (Object[])AssetDatabase.LoadAllAssetsAtPath(prefab);
59             int converted = 0;
60             foreach (Object obj in objs)
61             {
62                 if (obj != null && obj.GetType() == typeof(GameObject))
63                     converted += ConvertNetworkView(((GameObject)obj).GetComponents(), false);
64             }
65             if (movePrefabs && converted > 0)
66             {
67                 //This prefab needs to be under the root of a Resources folder!
68                 string path = prefab.Replace("\\", "/");
69                 int lastSlash = path.LastIndexOf("/");
70                 int resourcesIndex = path.LastIndexOf("/Resources/");
71                 if (resourcesIndex != lastSlash - 10)
72                 {
73                     if (path.Contains("/Resources/"))
74                     {
75                         Debug.LogWarning("Warning, prefab [" + prefab + "] was already in a resources folder. But has been placed in the root of another one!");
76                     }
77                     //This prefab NEEDS to be placed under a resources folder
78                     string resourcesFolder = path.Substring(0, lastSlash) + "/Resources/";
79                     EnsureFolder(resourcesFolder);
80                     string newPath = resourcesFolder + path.Substring(lastSlash + 1);
81                     string error = AssetDatabase.MoveAsset(prefab, newPath);
82                     if (error != "")
83                         Debug.LogError(error);
84                     Output("Fixed prefab [" + prefab + "] by moving it into a resources folder.");
85                 }
86             }
87         }
88
89         //Convert NetworkViews to PhotonViews in scenes
90         string[] sceneFiles = Directory.GetFiles("Assets/", "*.unity", SearchOption.AllDirectories);
91         foreach (string sceneName in sceneFiles)
92         {
93             EditorApplication.OpenScene(sceneName);
94             EditorUtility.DisplayProgressBar("Converting..", "Scene:" + sceneName, 0.2f);
95
96             int converted2 = ConvertNetworkView((NetworkView[])GameObject.FindObjectsOfType(typeof(NetworkView)), true);
97             if (converted2 > 0)
98             {
99                 //This will correct all prefabs: The prefabs have gotten new components, but the correct ID's were lost in this case
100                 PhotonViewHandler.HierarchyChange(); //TODO: most likely this is triggered on change or on save
101
102                 Output("Replaced " + converted2 + " NetworkViews with PhotonViews in scene: " + sceneName);
103                 EditorApplication.SaveScene(EditorApplication.currentScene);
104             }
105
106         }
107
108         //Convert C#/JS scripts (API stuff)
109         List scripts = GetScriptsInFolder("Assets");
110
111         EditorUtility.DisplayProgressBar("Converting..", "Scripts..", 0.9f);
112         ConvertScripts(scripts);
113
114         Output(EditorApplication.timeSinceStartup + " Completed conversion!");
115         EditorUtility.ClearProgressBar();
116
117         EditorUtility.DisplayDialog("Completed the conversion", "Don't forget to add \"PhotonNetwork.ConnectWithDefaultSettings();\" to connect to the Photon server before using any multiplayer functionality.", "OK");
118     }
File name: PhotonViewHandler.cs Copy
173     //TODO: check if this can be internal protected (as source in editor AND as dll)
174     public static void LoadAllScenesToFix()
175     {
176         string[] scenes = System.IO.Directory.GetFiles(".", "*.unity", SearchOption.AllDirectories);
177
178         foreach (string scene in scenes)
179         {
180             EditorApplication.OpenScene(scene);
181
182             PhotonViewHandler.HierarchyChange();//TODO: most likely on load also triggers a hierarchy change
183
184             EditorApplication.SaveScene();
185         }
186
187         Debug.Log("Corrected scene views where needed.");
188     }
File name: PhotonAnimatorView.cs Copy
17     {
18         Float = 1,
19         Int = 3,
20         Bool = 4,
21         Trigger = 9,
22     }
File name: PhotonAnimatorView.cs Copy
234     void SerializeDataContinuously()
235     {
236         if( m_Animator == null )
237         {
238             return;
239         }
240
241         for( int i = 0; i < m_SynchronizeLayers.Count; ++i )
242         {
243             if( m_SynchronizeLayers[ i ].SynchronizeType == SynchronizeType.Continuous )
244             {
245                 m_StreamQueue.SendNext( m_Animator.GetLayerWeight( m_SynchronizeLayers[ i ].LayerIndex ) );
246             }
247         }
248
249         for( int i = 0; i < m_SynchronizeParameters.Count; ++i )
250         {
251             SynchronizedParameter parameter = m_SynchronizeParameters[ i ];
252
253             if( parameter.SynchronizeType == SynchronizeType.Continuous )
254             {
255
256                 switch( parameter.Type )
257                 {
258                 case ParameterType.Bool:
259                     m_StreamQueue.SendNext( m_Animator.GetBool( parameter.Name ) );
260                     break;
261                 case ParameterType.Float:
262                     m_StreamQueue.SendNext( m_Animator.GetFloat( parameter.Name ) );
263                     break;
264                 case ParameterType.Int:
265                     m_StreamQueue.SendNext( m_Animator.GetInteger( parameter.Name ) );
266                     break;
267                 case ParameterType.Trigger:
268
269                     break;
270                 }
271             }
272         }
273     }
File name: PhotonAnimatorView.cs Copy
275     void DeserializeDataContinuously()
276     {
277         if( m_StreamQueue.HasQueuedObjects() == false )
278         {
279             return;
280         }
281
282         for( int i = 0; i < m_SynchronizeLayers.Count; ++i )
283         {
284             if( m_SynchronizeLayers[ i ].SynchronizeType == SynchronizeType.Continuous )
285             {
286                 m_Animator.SetLayerWeight( m_SynchronizeLayers[ i ].LayerIndex, (float)m_StreamQueue.ReceiveNext() );
287             }
288         }
289
290         for( int i = 0; i < m_SynchronizeParameters.Count; ++i )
291         {
292             SynchronizedParameter parameter = m_SynchronizeParameters[ i ];
293
294             if( parameter.SynchronizeType == SynchronizeType.Continuous )
295             {
296                 switch( parameter.Type )
297                 {
298                 case ParameterType.Bool:
299                     m_Animator.SetBool( parameter.Name, (bool)m_StreamQueue.ReceiveNext() );
300                     break;
301                 case ParameterType.Float:
302                     m_Animator.SetFloat( parameter.Name, (float)m_StreamQueue.ReceiveNext() );
303                     break;
304                 case ParameterType.Int:
305                     m_Animator.SetInteger( parameter.Name, (int)m_StreamQueue.ReceiveNext() );
306                     break;
307                 case ParameterType.Trigger:
308
309                     break;
310                 }
311             }
312         }
313     }

Download file with original file name:Trigger

Trigger 122 lượt xem

Gõ tìm kiếm nhanh...