跳至主要内容

Scripting

Code Snippets

The namespace
using ChocDino.PartyIO;

Examples

Example of low-level enumeration of all mice
using System.Collections.Generic;
using UnityEngine;
using ChocDino.PartyIO;

public class Example : MonoBehaviour
{
void Awake()
{
MouseDeviceManager.ChangedConnectionState += OnMouseConnectionChanged;
}

void Update()
{
MouseDeviceManager.Instance.Update();
LogMiceState();
}

void LogMiceState()
{
List<MouseDevice> mice = MouseDeviceManager.Instance.All;
foreach (var mouse in mice)
{
string text = string.Format("Id: {0} Connection: {1} Position: {2} Scroll; {3}", mouse.DeviceId, mouse.ConnectionState, mouse.PositionDelta, mouse.ScrollDelta);
Debug.Log(text);

if (mouse.IsPressed(MouseButton.Left))
{
Debug.Log("Left button is pressed");
}
if (mouse.WasPressedThisFrame(MouseButton.Left))
{
Debug.Log("Left button was pressed this frame");
}
if (mouse.WasReleasedThisFrame(MouseButton.Left))
{
Debug.Log("Left button was released this frame");
}
}
}

void OnDestroy()
{
MouseDeviceManager.Instance.Dispose();
}

void OnMouseConnectionChanged(MouseDevice mouse)
{
if (mouse.ConnectionState == MouseConnectionState.Connected)
{
Debug.Log("Mouse with id " + mouse.DeviceId + " just connected: (" + mouse.FriendlyName + " | " + mouse.ManufacturerName + ")");
}
else if (mouse.ConnectionState == MouseConnectionState.Disconnected)
{
Debug.Log("Mouse with id " + mouse.DeviceId + " just disconnected.");
}
}
}
Example of high-evel usage by inheriting from a cursor manager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using ChocDino.PartyIO;

public class CustomCursorManagerDemo : MouseCursorManager_Canvas
{
private Color[] _cursorColors = { Color.white, Color.red, Color.green, Color.blue };
private int _cursorSpawnCount;

// Note: user must set the DefaultCursorImage and CursorParent properties.

protected override void Awake()
{
// Here we can replace or extend the base logic
base.Awake();
}

protected override void OnEnable()
{
// Here we can replace or extend the base logic
base.OnEnable();
}

protected override void OnDisable()
{
// Here we can replace or extend the base logic
base.OnDisable();
}

protected override void OnApplicationFocus(bool hasFocus)
{
// Here we can replace or extend the base logic
base.OnApplicationFocus(hasFocus);
}

protected override void OnApplicationPause(bool pauseStatus)
{
// Here we can replace or extend the base logic
base.OnApplicationPause(pauseStatus);
}

protected override void Update()
{
// Run the base cursor manager logic
base.Update();

// Custom logic for spawning and destroying cursors based on left/right click.
var mice = _mouseDeviceManager.All;
foreach (var mouse in mice)
{
if (mouse.WasPressedThisFrame(MouseButton.Left))
{
AddCursor(mouse);
}
else if (mouse.WasPressedThisFrame(MouseButton.Right))
{
RemoveCursor(mouse, removeFromList:true);
}
}
}

protected override BaseMouseCursor CreateCursor(MouseDevice mouse)
{
// We override this method to add some custom cursor color logic

// Create the cursor state
var result = base.CreateCursor(mouse);

// Override cursor properties
result.Color = _cursorColors[_cursors.Count % _cursorColors.Length];

return result;
}

protected override void UpdateCursor(BaseMouseCursor cursorBase)
{
// This is called whenever a cursor is updated.
// Here we could update any cursor properties, eg Color, Image etc.
base.UpdateCursor(cursorBase);
}

protected override void DestroyCursor(BaseMouseCursor cursorBase)
{
// This is called whenever a cursor is destroyed.
base.DestroyCursor(cursorBase);
}
}