Posted on

How to Use Photon Chat for Unity Lesson 2

For this lesson on how to use the photon chat plugin, I will show you how to set up your Unity project with the photon chat plugin. To get started you will need to open the project you wish to use and navigate to the Unity Asset Store. Now there are two packages that you can install into your project. You can get just the basic Photon Chat plugin or if you are already building a multiplayer game with the PUN 2 plugin then you can just use that as it comes with the chat assets. Once you have imported the chat plugin you need to go to the official Photon site and create or sign in to your account. You will then need to create a new chat app ID then copy the ID and go back to Unity. inside Unity, you will need to locate your Photon Server Settings and paste the app ID into where it says App ID Chat.

Posted on

Make an Audio Mute Toggle in Unity

In this video, we announce the winners of our first Snake Cubed competition. We have also cleared the leaderboards and started a new competition for this next week.

For the tutorial of this video, we show you how to create a new game mechanic which is a mute button that will mute all audio in your game. This game mechanic is quite simple to make. All you need is a speaker icon to create the UI Toggle out of and then a little bit of code to make it work. You can then create a prefab out of this mechanic and use it in future Unity projects.

MuteToggle.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using UnityEngine.UI;

[RequireComponent(typeof(Toggle))]
public class MuteToggle : MonoBehaviour
{
    Toggle myToggle;

    // Start is called before the first frame update
    void Start()
    {
        myToggle = GetComponent<Toggle>();
        if(AudioListener.volume == 0)
        {
            myToggle.isOn = false;
        }
    }

    public void ToggleAudioOnValueChange(bool audioIn)
    {
        if(audioIn)
        {
            AudioListener.volume = 1;
        }
        else
        {
            AudioListener.volume = 0;
        }
    }
}
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
}
Posted on 11 Comments

Snake Cubed Competition and Daily Entry

Download on Android and IOS







*The android and ios version is free to play and is setup up for multiplayer but these versions are not yet set up to be used in this competition.

Competition is Currently Closed

Check out this competitions winners in the winner section of this post.

Please come back for future competitions and in the meantime check out some of our tutorials, code, and designs. https://www.infogamerhub.com/tutorials/ Designs: https://www.infogamerhub.com/design/

Please Invite and Challenge Your Friends!

We want to make it really simple for you to invite and challenge your friends. Copy the text and paste when you click to share via Facebook, Twitter, etc. Put your display name and high score in the blanks and invite some friends you want to challenge.

Can you beat my #HighScore of ___!?!? There’s a current competition running and you can join. My display name is ___ and I challenge you! Let me know your display name in the comments. #SnakeCubed #Competition #Challenge #Play https://www.infogamerhub.com/snake-cubed-competition/

Win an Amazon Gift Card – see details below

Unity WebGL Player | SnakeCubed


Super Important Tips for First-Timers!

  • New to InfoGamer? Register for your free account here.
  • Do not use your email address for your in-game display name. Otherwise, random people could email you. Display names are used in-game and posted in the winner’s section.
  • Your display name is permanent.
  • To pick colors or scroll the high scoreboard click and drag instead of using the mouse wheel.
  • Infogamerhub.com will never ask for passwords or personal account information.
  • Warning turning on the edges of the cube can make the snake leave the cube, be careful. 😉
  • Nathan and Mark like to compete and play our games too, but don’t worry our high scores and daily entries don’t count towards winning the prizes. See if you can beat our high scores! Our display names are Nathan IGH (DEV) and Mark IGH (DEV).
  • The more subscribers we get, the better prizes we can offer, so please share this post!
  • See below for details about the competition rules and regulations.

Trouble Shooting

  • WebGL is not supported on mobile devices but if you go to setting and use the view as a desktop version it seems to work on my phone.
  • If you are experiencing other troubles with Snake Cubed Please leave us a comment at the bottom of the page.
  • If you don’t see the leader board on the game after logging in your high scores won’t be recorded. Let us know of the issue with your game display name and we will try and help.

How to Play

Controls: To turn the snake, use keys A and D or the Left and Right arrows. If you have a touch screen, you can touch the left and right side of the game screen to turn those directions.

Objective: The snake slithers around the cube. Control the snake to collect blocks which will make your snake grow. The longest snake gets the highest score.

Warnings: Running the snake into its own body will end the game. Turning the snake the wrong direction on the edges of the cube can result in the snake leaving the playfield. This is difficult to recover from and will also likely end the game.

Become a Member

We want to bring more tutorials, models, packages, games, group projects, prizes and competitions to you. We are looking into more ways to include members in our game development and our gaming community. Click below to become a member.

Rules

  • Have fun!
  • Keep it clean. Display names including profanity, hate speech, and vulgar references won’t be eligible to win.
  • The login to our website and the login to the in-game account is required but the paid subscription is not necessary for daily entry.
  • Each player can submit ONE ENTRY PER DAY during the competition by logging in to the Snake Cubed game.
  • Players can log in and play multiple times per day but only one daily entry will be counted towards the competition.
  • Competition and daily entry are only available on WebGL. Future competitions may be available on additional platforms.
  • The competition will allow for two winners. A prize will be issued to the player with the highest score. A prize will also be issued to a randomly selected player who entered the competition daily. Winners will be determined and contacted via email within 24 hours of the competition ending. If there is no response from a winner after three days we will select the next eligible winner.
  • In the event of a tie, the highest score winner will be determined by random selection.
  • Only one prize issued per player per month.
  • For any unforeseen issues and errors with the competition, InfoGamer has the right to make any changes as needed.
  • High scores will be reset at the beginning of each competition.
  • Players found cheating will be excluded from winning prizes.

Prizes

  • The highest score will receive $20.00 Amazon gift card
  • Randomly selected daily entries $20.00 Amazon gift card

Winners

Winners are being contacted via email and have 3 days to respond to our email.

High Score Winners 4/7/2020: (due to a minor issue, Nathan and Mark have decided there are two high score winners for the competition ending 4/7/2020)

Congrats to both lolomagz100 with a 176 and MrJohnWeez at 172 it was a very close game.

Random Daily Entry Winner 4/7/2020:

Congrats to our Random Daily Entry Dad1

High Score Winner 4/15/2020:

Congrats madyoshi with a winning high score of 255.

Random Daily Entry Winner 4/15/2020:

Congrats to our Random Daily Entry bsmith950.

Potential Future Prizes

We have ideas for potential prizes in future competitions and would like your input. Some of the links below are for products we’ve used and would recommend. Others have good reviews. Leave us a comment at the bottom of the page with your suggestions for future prizes. We’re still thinking this through, but to avoid exorbitant shipping costs, some prizes may be sent as an equivalent Amazon gift card in lieu of the listed prize. Links below are affiliate links.

I recently picked up the Logitech MX keyboard and have found it to be a great keyboard. It’s responsive and easy to switch between different devices. I have not tested this keyboard for gaming or longevity but it is pretty awesome to use one keyboard instead of 3. What do you guys think of this being a future prize?

We could also offer a $100 Amazon gift card and you could put it towards a monitor like this one. It has good specs and pretty good reviews.

We have been using a similar Blue microphone for our recordings and gaming and have found this microphone to be sufficient. This could be another option for a future prize.

Best of luck winning the prizes. We need your help to continue awarding prizes and setting up other competitions like this one. Please remember to share this Snake Cubed Competition post and thanks for reading.