Four Quick Tricks for Unity3D Development

More flexibility, less code - more productive development.









Unity3D β€” , 8  β€” , , -. , Unity -, , .





, , , - β€” , . - 24 72 ,  β€” , GitHub Game Off, .





-, C++ ( , ), , :  β€” .





 β€” (, , ) β€” :





  1. : , .





  2. : , .





Unity : Unity, .





, Unity3D .





: Unity 3D Technologies ( ).





1.

 β€” . Unity .





 β€” [Serializable] .  β€” Unity:





[Serializable]
public struct PlayerStats
{
   public int movementSpeed;
   public int hitPoints;
   public bool hasHealthPotion;
}
      
      



, , .





List of player stats in the Unity property inspector
Unity

: , ,  β€” , .





( )  β€” , . :





List of player stats with sprites and enums in the Unity inspector
Unity
public enum PlayerType
{
    ARCHER, KNIGHT
}

[Serializable]
public struct PlayerStats
{
    public int movementSpeed;
    public int hitPoints;
    public bool hasHealthPotion;
    public Sprite face;
    public PlayerType type;
}
      
      



, Unity  β€” .





2. RequireComponent

 β€” . , , , Rigidbody .  β€” RequireComponent.





:





  1. .





  2. .





  3. , .





RequireComponent :





[RequireComponent(typeof(Rigidbody))]
public class PlayerScript : MonoBehaviour
{
    Rigidbody rigidbody;

    void Awake()
    {
        rigidbody = GetComponent<Rigidbody>();
    }
}
      
      



. Awake Start, .





. , RequireComponent . ,  β€” .





, , null, β€” , .





3.

, , : Unity . Unity , , , , .





,  β€” .





OnClick: , , .





, , onClick Unity, . :  β€” ( setActive(true) ),  β€” ( play() ) Animator β€” ( setTrigger() ).  β€” .





Example of a list with OnClick events
OnClick

. . . , . :  β€” , .





4. Unity

Unity UnityEvent, OnClick Unity. UnityEvent, , , OnClick:





One UnityEvent variable named EventsToBeCalled
UnityEvent EventsToBeCalled

, , UnityEvent . , UnityEvent Invoke:





using UnityEngine;
using UnityEngine.Events;
public class CallEventsScript : MonoBehaviour
{
   public UnityEvent eventsToBeCalled;
   public void CallEvents()
   {
      eventsToBeCalled.Invoke();
   }
}
      
      



CallEvents UnityEvent. , , .  β€” .





Animation timeline with added event that calls the CallEvents method
, CallEvents

UnityEvent , , , Awake, Start, OnEnable, OnDisable  . . , , Start, β€” .





 β€” , : , . UnityEvent:





[RequireComponent(typeof(Collider))]
public class TriggerBoxScript : MonoBehaviour
{
    public UnityEvent eventsToBeCalledOnCollision;
    public List<string> objectsTagToActivate;

    private void OnCollisionEnter(Collision other)
    {
        if (OtherHasWantedTag(other.gameObject))
        {
            InvokeEvents();
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (OtherHasWantedTag(other.gameObject))
        {
            InvokeEvents();
        }
    }

    private bool OtherHasWantedTag(GameObject other)
    {
        var found = objectsTagToActivate.Find(other.CompareTag);
        return found != null;
    }

    private void InvokeEvents()
    { 
        eventsToBeCalledOnCollision.Invoke();   
    }
}
      
      



, ( OnTriggerEnter, OnCollisionEnter). , .





Example of required components for a trigger box

, . , Β«PlayerΒ»  β€” , . : , , ,  . .





,  β€” Unity.  β€” .





(OnClick UnityEvent) ( ) ( ).





, , : . , , , . Unity, .





,  β€” , , . , , .





 .





Coast, brown sand.  Albert Edelfelt (1935) [USEUM]
, . (1935) [USEUM]

Alconost.





Alconost , 70 . - , , API, , 24/7, .





β€” , , , , , , , Google Play App Store.








All Articles