Posted on

Cross Platform Native Plugins: Essential Kit – Setup in Unity

Download

Documentation: https://assetstore.essentialkit.voxelbusters.com/

0:00 Welcome to the Setup for CPNP2
0:39 Purchase and Plugin Setup
2:30 Documentation
4:24 Introduction and Application Setup

In this tutorial series, we will show you how to use all the features of the Cross Platform Native Plugins 2 in Unity. This Unity Package Developed by Voxel Busters can help you implement many standard features that can be found in almost every popular mobile game out there.

Posted on

How to Code a Zelda Ball Puzzle in Unity

Lately, I have been replaying through Zelda Breath of the Wild. If you are familiar with the game then you will probably know about the various ball puzzles that you have to solve in order to complete shrines and gain spirit orbs. These puzzles often require you to drop a special ball into a socket after which everything will change color and a door will unlock. In this lesson, I will show you how to replicate this game mechanic.

For this game mechanic, we will use a Unity Event which makes it so we can assign any public function to be called when the puzzle is completed.

Unlock Code and Member Content

ZeldaGoal.cs

using UnityEngine;
using UnityEngine.Events;

public class ZeldaGoal : MonoBehaviour
{
    
    [SerializeField] GameObject key;

   
    public UnityEvent onUnlock;


    public void UnlockEventTriggered()
    {
        onUnlock.Invoke();
    }

    private void OnTriggerEnter(Collider other)
    {
        if(other.gameObject == key)
        {
            UnlockEventTriggered();
        }
    }

    public void TestUnlock()
    {
        Debug.Log("GOAL!");
    }
}
using Photon.Chat;
using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PhotonChatManager : MonoBehaviour, IChatClientListener
{
    #region Setup

    [SerializeField] GameObject joinChatButton;
    ChatClient chatClient;
    bool isConnected;
    [SerializeField] string username;

    public void UsernameOnValueChange(string valueIn)
    {
        username = valueIn;
    }

    public void ChatConnectOnClick()
    {
        isConnected = true;
        chatClient = new ChatClient(this);
        //chatClient.ChatRegion = "US";
        chatClient.Connect(PhotonNetwork.PhotonServerSettings.AppSettings.AppIdChat, PhotonNetwork.AppVersion, new AuthenticationValues(username));
        Debug.Log("Connenting");
    }

    #endregion Setup

    #region General

    [SerializeField] GameObject chatPanel;
    string privateReceiver = "";
    string currentChat;
    [SerializeField] InputField chatField;
    [SerializeField] Text chatDisplay;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (isConnected)
        {
            chatClient.Service();
        }

        if (chatField.text != "" && Input.GetKey(KeyCode.Return))
        {
            SubmitPublicChatOnClick();
            SubmitPrivateChatOnClick();
        }
    }

    #endregion General

    #region PublicChat

    public void SubmitPublicChatOnClick()
    {
        if (privateReceiver == "")
        {
            chatClient.PublishMessage("RegionChannel", currentChat);
            chatField.text = "";
            currentChat = "";
        }
    }

    public void TypeChatOnValueChange(string valueIn)
    {
        currentChat = valueIn;
    }

    #endregion PublicChat

    #region PrivateChat

    public void ReceiverOnValueChange(string valueIn)
    {
        privateReceiver = valueIn;
    }

    public void SubmitPrivateChatOnClick()
    {
        if (privateReceiver != "")
        {
            chatClient.SendPrivateMessage(privateReceiver, currentChat);
            chatField.text = "";
            currentChat = "";
        }
    }

    #endregion PrivateChat

    #region Callbacks

    public void DebugReturn(DebugLevel level, string message)
    {
        //throw new System.NotImplementedException();
    }

    public void OnChatStateChange(ChatState state)
    {
        if(state == ChatState.Uninitialized)
        {
            isConnected = false;
            joinChatButton.SetActive(true);
            chatPanel.SetActive(false);
        }
    }

    public void OnConnected()
    {
        Debug.Log("Connected");
        joinChatButton.SetActive(false);
        chatClient.Subscribe(new string[] { "RegionChannel" });
    }

    public void OnDisconnected()
    {
        isConnected = false;
        joinChatButton.SetActive(true);
        chatPanel.SetActive(false);
    }

    public void OnGetMessages(string channelName, string[] senders, object[] messages)
    {
        string msgs = "";
        for (int i = 0; i < senders.Length; i++)
        {
            msgs = string.Format("{0}: {1}", senders[i], messages[i]);

            chatDisplay.text += "\n" + msgs;

            Debug.Log(msgs);
        }

    }

    public void OnPrivateMessage(string sender, object message, string channelName)
    {
        string msgs = "";

        msgs = string.Format("(Private) {0}: {1}", sender, message);

        chatDisplay.text += "\n " + msgs;

        Debug.Log(msgs);
        
    }

    public void OnStatusUpdate(string user, int status, bool gotMessage, object message)
    {
        throw new System.NotImplementedException();
    }

    public void OnSubscribed(string[] channels, bool[] results)
    {
        chatPanel.SetActive(true);
    }

    public void OnUnsubscribed(string[] channels)
    {
        throw new System.NotImplementedException();
    }

    public void OnUserSubscribed(string channel, string user)
    {
        throw new System.NotImplementedException();
    }

    public void OnUserUnsubscribed(string channel, string user)
    {
        throw new System.NotImplementedException();
    }

    #endregion Callbacks
}

Posted on

How to Code a Kill Feed UI in Unity

For this lesson on how to create game mechanics for Unity, I will show you how to create a Kill Feed UI as found in most first-person shooters and other fighting games. By the end of this tutorial, you will have a working kill feed prefab that you can add to any Unity project. This prefab is also very easy to use. Just add it to any scene you which to use it in and then there are three options for using this mechanic. You can have if display just names, names and how, or names and an icon of how each player was kill. There is then a public function for each of these options that you can call from anywhere in your project using the singleton of the prefab.

Unlock Code and Member Content

KillListing.cs

using UnityEngine;
using UnityEngine.UI;

//This script is attached to the kill listing prefab
public class KillListing : MonoBehaviour
{
    [SerializeField] Text killerDisplay; //The UI Text that displays the killer name
    [SerializeField] Text killedDisplay; //The UI Text that displays the Killed name

    [SerializeField] Text howDisplay;
    [SerializeField] Image howImageDisplay;

    private void Start()
    {
        Destroy(gameObject, 10f); //To keep our feed from getting too long we destroy old listings after a given time.
    }

    //This function is called by the AddNewKillListing function to set the two text variables.
    public void SetNames(string killerName, string killedName)
    {
        killerDisplay.text = killerName;
        killedDisplay.text = killedName;
    }

    public void SetNamesAndHow(string killerName, string killedName, string how)
    {
        killerDisplay.text = killerName;
        killedDisplay.text = killedName;

        howDisplay.text = how;
    }

    public void SetNamesAndHowImage(string killerName, string KilledName, Sprite howImage)
    {
        killerDisplay.text = killerName;
        killedDisplay.text = KilledName;
        howImageDisplay.sprite = howImage;
    }
}

KillFeed.cs

using UnityEngine;


//This script is attached to the Kill Feed object and is used to control the feed.
public class KillFeed : MonoBehaviour
{
    public static KillFeed instance; //Singleton of this script so you can call the AddNewKillListing function from anywhere in your project.
    [SerializeField] GameObject killListingPrefab; //This is a variable to hold the kill listing prefab.
    [SerializeField] GameObject killListingWithImagePrefab; //This is a variable to hold the kill listing prefab.
    [SerializeField] Sprite[] howImages;

    private void Start()
    {
        instance = this;
    }

    //To call this function use the following example code
    //KillFeed.instance.AddNewKillListing("KillerName", "KilledName");
    public void AddNewKillListing(string killer, string killed)
    {
        GameObject temp = Instantiate(killListingPrefab, transform);
        temp.transform.SetSiblingIndex(0);
        KillListing tempListing = temp.GetComponent<KillListing>();
        tempListing.SetNames(killer, killed);
    }


    //To call this function use the following example code
    //KillFeed.instance.AddNewKillListingWithHow("KillerName", "KilledName", "Splattered");
    public void AddNewKillListingWithHow(string killer, string killed, string how)
    {
        GameObject temp = Instantiate(killListingPrefab, transform);
        temp.transform.SetSiblingIndex(0);
        KillListing tempListing = temp.GetComponent<KillListing>();
        tempListing.SetNamesAndHow(killer, killed, how);
    }

    //To call this function use the following example code
    //KillFeed.instance.AddNewKillListingWithHowImage("KillerName", "KilledName", 0);
    public void AddNewKillListingWithHowImage(string killer, string killed, int howIndex)
    {
        GameObject temp = Instantiate(killListingWithImagePrefab, transform);
        temp.transform.SetSiblingIndex(0);
        KillListing tempListing = temp.GetComponent<KillListing>();
        tempListing.SetNamesAndHowImage(killer, killed, howImages[howIndex]);
    }
}
using Photon.Chat;
using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PhotonChatManager : MonoBehaviour, IChatClientListener
{
    #region Setup

    [SerializeField] GameObject joinChatButton;
    ChatClient chatClient;
    bool isConnected;
    [SerializeField] string username;

    public void UsernameOnValueChange(string valueIn)
    {
        username = valueIn;
    }

    public void ChatConnectOnClick()
    {
        isConnected = true;
        chatClient = new ChatClient(this);
        //chatClient.ChatRegion = "US";
        chatClient.Connect(PhotonNetwork.PhotonServerSettings.AppSettings.AppIdChat, PhotonNetwork.AppVersion, new AuthenticationValues(username));
        Debug.Log("Connenting");
    }

    #endregion Setup

    #region General

    [SerializeField] GameObject chatPanel;
    string privateReceiver = "";
    string currentChat;
    [SerializeField] InputField chatField;
    [SerializeField] Text chatDisplay;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (isConnected)
        {
            chatClient.Service();
        }

        if (chatField.text != "" &amp;&amp; Input.GetKey(KeyCode.Return))
        {
            SubmitPublicChatOnClick();
            SubmitPrivateChatOnClick();
        }
    }

    #endregion General

    #region PublicChat

    public void SubmitPublicChatOnClick()
    {
        if (privateReceiver == "")
        {
            chatClient.PublishMessage("RegionChannel", currentChat);
            chatField.text = "";
            currentChat = "";
        }
    }

    public void TypeChatOnValueChange(string valueIn)
    {
        currentChat = valueIn;
    }

    #endregion PublicChat

    #region PrivateChat

    public void ReceiverOnValueChange(string valueIn)
    {
        privateReceiver = valueIn;
    }

    public void SubmitPrivateChatOnClick()
    {
        if (privateReceiver != "")
        {
            chatClient.SendPrivateMessage(privateReceiver, currentChat);
            chatField.text = "";
            currentChat = "";
        }
    }

    #endregion PrivateChat

    #region Callbacks

    public void DebugReturn(DebugLevel level, string message)
    {
        //throw new System.NotImplementedException();
    }

    public void OnChatStateChange(ChatState state)
    {
        if(state == ChatState.Uninitialized)
        {
            isConnected = false;
            joinChatButton.SetActive(true);
            chatPanel.SetActive(false);
        }
    }

    public void OnConnected()
    {
        Debug.Log("Connected");
        joinChatButton.SetActive(false);
        chatClient.Subscribe(new string[] { "RegionChannel" });
    }

    public void OnDisconnected()
    {
        isConnected = false;
        joinChatButton.SetActive(true);
        chatPanel.SetActive(false);
    }

    public void OnGetMessages(string channelName, string[] senders, object[] messages)
    {
        string msgs = "";
        for (int i = 0; i &lt; senders.Length; i++)
        {
            msgs = string.Format("{0}: {1}", senders[i], messages[i]);

            chatDisplay.text += "\n" + msgs;

            Debug.Log(msgs);
        }

    }

    public void OnPrivateMessage(string sender, object message, string channelName)
    {
        string msgs = "";

        msgs = string.Format("(Private) {0}: {1}", sender, message);

        chatDisplay.text += "\n " + msgs;

        Debug.Log(msgs);
        
    }

    public void OnStatusUpdate(string user, int status, bool gotMessage, object message)
    {
        throw new System.NotImplementedException();
    }

    public void OnSubscribed(string[] channels, bool[] results)
    {
        chatPanel.SetActive(true);
    }

    public void OnUnsubscribed(string[] channels)
    {
        throw new System.NotImplementedException();
    }

    public void OnUserSubscribed(string channel, string user)
    {
        throw new System.NotImplementedException();
    }

    public void OnUserUnsubscribed(string channel, string user)
    {
        throw new System.NotImplementedException();
    }

    #endregion Callbacks
}