Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Kolkhoze;
- using System.Text;
- using System;
- using System.Runtime.InteropServices;
- namespace Kolkhoze {
- public class AlienFX : MonoBehaviour {
- bool _isActive = false;
- bool isActive { get { return _isActive; } }
- uint _nbDevice = 0;
- uint nbDevice { get { return _nbDevice; } }
- uint _nbLights = 0;
- uint nbLights { get { return _nbLights; } }
- // Use this for initialization
- void Start () {
- _isActive = Init ();
- if (isActive) {
- StartCoroutine (ColorGradient (
- Color.blue, Color.red, 0));
- }
- }
- /// <summary>
- /// Init AlienFX dll.
- /// </summary>
- bool Init () {
- if (AlienWrapper.LFX_Initialize () != AlienWrapper.LFX_SUCCESS) {
- Debug.Log ("AlienFX : Init failed");
- return false;
- }
- if (AlienWrapper.LFX_Reset () != AlienWrapper.LFX_SUCCESS) {
- Debug.Log ("AlienFX : Reset failed");
- return false;
- }
- if (AlienWrapper.LFX_GetNumDevices (ref _nbDevice) != AlienWrapper.LFX_SUCCESS) {
- Debug.Log ("AlienFX : GetNumDevices failed");
- return false;
- }
- if (_nbDevice == 0) {
- Debug.Log ("AlienFX : Found no AlienFX device");
- return false;
- }
- if (AlienWrapper.LFX_GetNumLights (0, ref _nbLights) != AlienWrapper.LFX_SUCCESS) {
- Debug.Log ("AlienFX : GetNumLights failed");
- return false;
- }
- Debug.Log ("AlienFX : Found " + _nbLights + " lights");
- return true;
- }
- /// <summary>
- /// Oscillate gradually between two colors
- /// </summary>
- public IEnumerator ColorGradient(Color color1, Color color2, float duration = 0) {
- float animDuration = 0.5f;
- float globalTime = 0;
- while (duration == 0 || globalTime <= duration) {
- float t = 0;
- while (t <= animDuration) {
- Color lerpedColor = Color.Lerp (color1, color2, t / animDuration);
- SetAllLights (ColorToLFX (lerpedColor));
- t += Time.deltaTime;
- yield return null;
- }
- t = 0;
- while (t <= animDuration) {
- Color lerpedColor = Color.Lerp (color2, color1, t / animDuration);
- SetAllLights (ColorToLFX (lerpedColor));
- t += Time.deltaTime;
- yield return null;
- }
- globalTime += 2 * animDuration;
- }
- yield break;
- }
- /// <summary>
- /// Sets all lights to a specific color
- /// </summary>
- public void SetAllLights(AlienWrapper.LFX_COLOR color) {
- for (uint lightIndex = 0; lightIndex < nbLights; lightIndex++) {
- AlienWrapper.LFX_SetLightColor (0, lightIndex, ref color);
- }
- AlienWrapper.LFX_Update ();
- }
- /// <summary>
- /// Utility function to translate Unity color to LFX color
- /// </summary>
- public static AlienWrapper.LFX_COLOR ColorToLFX(Color color) {
- return new AlienWrapper.LFX_COLOR (
- (byte)(color.r * 255),
- (byte)(color.g * 255),
- (byte)(color.b * 255),
- (byte)(color.a * 255)
- );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement