NetPad
  • General
    • Introduction
    • Technical Overview
  • Manual
    • Terminology
      • Server (Game), Client (Controller)
      • Controller - Input, Proxy
      • Constants
      • Webserver
    • Components
      • NetPadServer
      • NetPadClient
      • NetPadBehaviour
      • Input
        • NetPadJoystick
        • NetPadGyroscope
        • NetPadDPad
          • DPadButton
        • NetPadAccelerometer
        • NetPadTouch
        • NetPadCamera
        • NetPadButton
      • LayoutListener
      • NetPadDiscoveryServer
      • NetPadDiscoveryClient
    • Other
      • Attributes
      • Build Pipeline
      • NetPad Settings
      • NetPad Constants
    • Tutorials
      • Getting Started
        • Project Setup
        • Game Setup
          • Preperation
          • Ball
          • Ball Spawner
          • Scene Setup
        • Controller Setup
          • Background - Player Properties
          • Scene Setup
        • Build
Powered by GitBook
On this page
  1. Manual
  2. Tutorials
  3. Getting Started
  4. Controller Setup

Background - Player Properties

Follow this step-by-step guide to create the given script:

Step 1: Create a new script

  • In your Unity project, right-click in the 'Assets' panel and select 'Create' > 'C# Script'. Name the script 'Background'.

Step 2: Open the script in an editor

  • Double-click the 'Background' script in the 'Assets' panel to open it in your preferred code editor.

Step 3: Add namespaces

  • At the top of the script, replace the existing 'using' statements with the following:

    using NetPad.Attributes;
    using NetPad.Client;
    using RollABall.Common;
    using UnityEngine;
    using UnityEngine.UI;

Step 4: Define the class and inherit from MonoBehaviour

  • Replace the 'MonoBehaviour' inheritance in the class definition to match the given script:

    public class Background : MonoBehaviour
    {
    }

Step 5: Add serialized fields and GetComponent attributes

  • Within the 'Background' class, add the following serialized fields and GetComponent attributes:

    [SerializeField, FindObjectOfType] private NetPadClient Client;
    [SerializeField, GetComponent] private Image BackgroundImage;

Step 6: Implement the Start method

  • Add a private Start method to subscribe to the client's PropertyChanged event:

    private void Start()
    {
        Client.Properties.PropertyChanged += OnPropertyChanged;
    }

Step 7: Implement the OnPropertyChanged method

  • Add the OnPropertyChanged method to handle property changes and update the background color:

    private void OnPropertyChanged(string key, object newValue, object oldValue)
    {
        if (NetPadConstants.Property.PlayerColor == key)
        {
            BackgroundImage.color = (Color)newValue;
        }
    }

Step 8: Save and return to Unity

  • Save the 'Background' script in your code editor and return to the Unity editor. The script will automatically compile and be ready for use.

The background color will change based on the player's color property received from the NetPad client.

PreviousController SetupNextScene Setup

Last updated 2 years ago