Advertisement
Guest User

Untitled

a guest
Jun 5th, 2017
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.93 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Kolkhoze;
  5. using System.Text;
  6. using System;
  7. using System.Runtime.InteropServices;
  8.  
  9. namespace Kolkhoze {
  10.     public class AlienFX : MonoBehaviour {
  11.  
  12.         bool _isActive = false;
  13.         bool isActive { get { return _isActive; } }
  14.  
  15.         uint _nbDevice = 0;
  16.         uint nbDevice { get { return _nbDevice; } }
  17.  
  18.         uint _nbLights = 0;
  19.         uint nbLights { get { return _nbLights; } }
  20.  
  21.         // Use this for initialization
  22.         void Start () {
  23.             _isActive = Init ();
  24.  
  25.             if (isActive) {
  26.                 StartCoroutine (ColorGradient (
  27.                     Color.blue, Color.red, 0));
  28.             }
  29.         }
  30.  
  31.         /// <summary>
  32.         /// Init AlienFX dll.
  33.         /// </summary>
  34.         bool Init () {
  35.             if (AlienWrapper.LFX_Initialize () != AlienWrapper.LFX_SUCCESS) {
  36.                 Debug.Log ("AlienFX : Init failed");
  37.                 return false;
  38.             }
  39.  
  40.             if (AlienWrapper.LFX_Reset () != AlienWrapper.LFX_SUCCESS) {
  41.                 Debug.Log ("AlienFX : Reset failed");
  42.                 return false;
  43.             }
  44.                
  45.             if (AlienWrapper.LFX_GetNumDevices (ref _nbDevice) != AlienWrapper.LFX_SUCCESS) {
  46.                 Debug.Log ("AlienFX : GetNumDevices failed");
  47.                 return false;
  48.             }
  49.  
  50.             if (_nbDevice == 0) {          
  51.                 Debug.Log ("AlienFX : Found no AlienFX device");
  52.                 return false;
  53.             }
  54.                
  55.             if (AlienWrapper.LFX_GetNumLights (0, ref _nbLights) != AlienWrapper.LFX_SUCCESS) {        
  56.                 Debug.Log ("AlienFX : GetNumLights failed");
  57.                 return false;
  58.             }          
  59.  
  60.             Debug.Log ("AlienFX : Found " + _nbLights + " lights");
  61.  
  62.             return true;
  63.         }
  64.  
  65.         /// <summary>
  66.         /// Oscillate gradually between two colors
  67.         /// </summary>
  68.         public IEnumerator ColorGradient(Color color1, Color color2, float duration = 0) {
  69.             float animDuration = 0.5f;
  70.             float globalTime = 0;
  71.             while (duration == 0 || globalTime <= duration) {
  72.                 float t = 0;
  73.                 while (t <= animDuration) {
  74.                     Color lerpedColor = Color.Lerp (color1, color2, t / animDuration);
  75.                     SetAllLights (ColorToLFX (lerpedColor));
  76.                     t += Time.deltaTime;
  77.                     yield return null;
  78.                 }
  79.  
  80.                 t = 0;
  81.  
  82.                 while (t <= animDuration) {
  83.                     Color lerpedColor = Color.Lerp (color2, color1, t / animDuration);
  84.                     SetAllLights (ColorToLFX (lerpedColor));
  85.                     t += Time.deltaTime;
  86.                     yield return null;
  87.                 }
  88.  
  89.                 globalTime += 2 * animDuration;
  90.             }
  91.  
  92.             yield break;
  93.         }
  94.  
  95.         /// <summary>
  96.         /// Sets all lights to a specific color
  97.         /// </summary>
  98.         public void SetAllLights(AlienWrapper.LFX_COLOR color) {
  99.             for (uint lightIndex = 0; lightIndex < nbLights; lightIndex++) {
  100.                 AlienWrapper.LFX_SetLightColor (0, lightIndex, ref color);
  101.             }
  102.  
  103.             AlienWrapper.LFX_Update ();
  104.         }
  105.  
  106.  
  107.         /// <summary>
  108.         /// Utility function to translate Unity color to LFX color
  109.         /// </summary>
  110.         public static AlienWrapper.LFX_COLOR ColorToLFX(Color color) {
  111.             return new AlienWrapper.LFX_COLOR (
  112.                 (byte)(color.r * 255),
  113.                 (byte)(color.g * 255),
  114.                 (byte)(color.b * 255),
  115.                 (byte)(color.a * 255)
  116.             );
  117.         }
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement