Advertisement
AnthonyGraceffa

QK Scaleform HUD Cam

Mar 7th, 2015
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.38 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Runtime.InteropServices;
  3. using System;
  4. using System.IO;
  5. using System.Collections;
  6. using Scaleform;
  7.  
  8. //SF 4.2 Intergration doc page 5. This script Extends SFCamera
  9. public class HUDCam : SFCamera {
  10.    
  11.     //Ref of the SWF to be loaded.
  12.     public gameHUD hudRef = null;
  13.     public string swfMovie;
  14.     ////////////////
  15.  
  16.  
  17.  
  18.     new public void Awake(){
  19.        
  20.     }
  21.    
  22.     // Hides the Start function in the base SFCamera. Will be called every time the ScaleformCamera (Main Camera game object)
  23.     // is created. Use new and not override, since return type is different from that of base::Start()
  24.     new public  IEnumerator Start(){
  25.         // The eval key must be set before any Scaleform related classes are loaded, other Scaleform Initialization will not
  26.         // take place.
  27.         #if (UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_EDITOR) && !UNITY_WP8
  28.         SF_SetKey("MC6PZR3PFKB4RWWTNRCUNF2EA4BCMJ8ZO5GAP7NQLSN0BCJSROC8R7DR1DTCMS5");
  29.         #elif UNITY_IPHONE
  30.         SF_SetKey("");
  31.         #elif UNITY_ANDROID
  32.         SF_SetKey("");
  33.         #elif UNITY_WP8
  34.         sf_setKey("");
  35.         #endif
  36.         //For GL based platforms - Sets a number to use for Unity specific texture management.  Adjust this number if
  37.         //you start to experience black and/or mssing textures.
  38.         #if UNITY_WP8
  39.         sf_setTextureCount(500);
  40.         #else
  41.         SF_SetTextureCount(500);
  42.         #endif
  43.        
  44.         InitParams.TheToleranceParams.Epsilon = 1e-5f;
  45.         InitParams.TheToleranceParams.CurveTolerance = 1.0f;
  46.         InitParams.TheToleranceParams.CollinearityTolerance = 10.0f;
  47.         InitParams.TheToleranceParams.IntersectionEpsilon = 1e-3f;
  48.         InitParams.TheToleranceParams.FillLowerScale = 0.0707f;
  49.         InitParams.TheToleranceParams.FillUpperScale = 100.414f;
  50.         InitParams.TheToleranceParams.FillAliasedLowerScale = 10.5f;
  51.         InitParams.TheToleranceParams.FillAliasedUpperScale = 200.0f;
  52.         InitParams.TheToleranceParams.StrokeLowerScale = 10.99f;
  53.         InitParams.TheToleranceParams.StrokeUpperScale = 100.01f;
  54.         InitParams.TheToleranceParams.HintedStrokeLowerScale = 0.09f;
  55.         InitParams.TheToleranceParams.HintedStrokeUpperScale = 100.001f;
  56.         InitParams.TheToleranceParams.Scale9LowerScale = 10.995f;
  57.         InitParams.TheToleranceParams.Scale9UpperScale = 100.005f;
  58.         InitParams.TheToleranceParams.EdgeAAScale = 0.95f;
  59.         InitParams.TheToleranceParams.MorphTolerance = 0.001f;
  60.         InitParams.TheToleranceParams.MinDet3D = 10.001f;
  61.         InitParams.TheToleranceParams.MinScale3D = 10.05f;
  62.         InitParams.UseSystemFontProvider = false;
  63.         return base.Start();
  64.     }
  65.    
  66.     // Application specific code goes here
  67.     new public void Update(){
  68.         CreateHud();
  69.         base.Update ();
  70.  
  71.     }
  72.    
  73.     private void CreateHud(){
  74.         if (hudRef == null){
  75.             SFMovieCreationParams creationParams = CreateMovieCreationParams("gameHUD.swf");
  76.             creationParams.TheScaleModeType = ScaleModeType.SM_ShowAll;
  77.             creationParams.IsInitFirstFrame = false;
  78.             hudRef = new gameHUD(this, SFMgr, creationParams);
  79.  
  80.             // easy indicator for if the HUD is loaded
  81.             Camera.main.GetComponent<HUDMaster>().hudActive = true;
  82.         }
  83.        
  84.     }
  85.  
  86.     //CALL THESE FUNCTIONS TO UPDATE HUD INFORMATION
  87.     //IMPORTANT Never call any of these in Start or Awake from a script that instantiates at the same
  88.     //time as the main camera ex: anything in the scene at the start, the swf won't be loaded in time and
  89.     //you will get a null reference exception
  90.  
  91.     //this is for testing, use updateBatterySmooth in final scripts
  92.     public void updateBattery(float batt){
  93.         hudRef.updateBattery (batt);
  94.     }
  95.  
  96.     //use this when you want to change what percent the bettery bar is displaying
  97.     //pass curBattery from HUDmaster as curBatt
  98.     public void updateBatterySmooth (float duration, float curBatt, float newBatt) {
  99.         StartCoroutine (lerpBattery (duration, curBatt, newBatt));
  100.     }
  101.  
  102.     //use this to update the objective text
  103.     public void updateObjective (string objective) {
  104.         hudRef.updateObjective (objective);
  105.     }
  106.  
  107.     // use this to set the compass objective indicator's position
  108.     public void setCompass (float duration, float curPos, float newPos) {
  109.         StartCoroutine (setCompassP1 (duration, curPos, newPos));
  110.     }
  111.  
  112.     //use this to set map X and Y
  113.     public void updateMapPos (float x, float y) {
  114.         hudRef.updateMap (x, y);
  115.     }
  116.  
  117.     //use this to slide ability bar left or right
  118.     public void slideAbilityBar (bool right, int ability) {
  119.         //checks if ability is changing
  120.         if(HUDMaster.Instance.abilitySwapping == false){
  121.             HUDMaster.Instance.abilitySwapping = true;
  122.             //finds new x position for the ability bar
  123.             float newX = 335 + ((float)ability * 45);
  124.             float curX = 335 + ((float)HUDMaster.Instance.curAbility * 45);
  125.  
  126.             StartCoroutine(lerpAbility(.5f,curX, newX));
  127.             HUDMaster.Instance.curAbility = ability;
  128.         }
  129.     }
  130.  
  131.  
  132.  
  133.     //These coroutines are called by their coresponding functions, this is for convenience, don't call these
  134.     private IEnumerator lerpBattery (float duration, float startP, float endP) {
  135.  
  136.         if (endP > startP){
  137.             duration *= (endP - startP) / 72;
  138.         }
  139.         else {
  140.             duration *= (startP - endP) / 72;
  141.         }
  142.  
  143.         float start = Time.time;
  144.         float elapsed = Time.time - start;
  145.         do
  146.         {  
  147.             elapsed = Time.time - start;
  148.             float normalisedTime = Mathf.Clamp(elapsed / duration, 0, 1);
  149.             float newBatt = Mathf.Lerp(startP, endP, normalisedTime);
  150.             hudRef.updateBattery(newBatt);
  151.             HUDMaster.Instance.curBattery = newBatt;
  152.             yield return null;
  153.         }
  154.         while(elapsed < duration);
  155.         hudRef.chargeGraphic();
  156.     }
  157.  
  158.  
  159.  
  160.     private IEnumerator setCompassP1( float duration, float startP, float endP)
  161.     {  
  162.         if (endP > startP){
  163.             duration *= (endP - startP) / 250;
  164.         }
  165.         else {
  166.             duration *= (startP - endP) / 250;
  167.         }
  168.  
  169.         float start = Time.time;
  170.         float elapsed = Time.time - start;
  171.         do
  172.         {  
  173.             elapsed = Time.time - start;
  174.             float normalisedTime = Mathf.Clamp(elapsed / duration, 0, 1);
  175.             float newPos = Mathf.Lerp(startP, endP, normalisedTime);
  176.             hudRef.setCompassP1(newPos);
  177.             HUDMaster.Instance.curCompassPos = newPos;
  178.             yield return null;
  179.         }
  180.         while(elapsed < duration);
  181.     }
  182.  
  183.     private IEnumerator lerpAbility (float duration, float startP, float endP) {
  184.  
  185.         float start = Time.time;
  186.         float elapsed = Time.time - start;
  187.         do
  188.         {  
  189.             elapsed = Time.time - start;
  190.             float normalisedTime = Mathf.Clamp(elapsed / duration, 0, 1);
  191.             float newX = Mathf.Lerp(startP, endP, normalisedTime);
  192.             hudRef.slideAbilitySmooth(newX);
  193.             yield return null;
  194.         }
  195.         while(elapsed < duration);
  196.         HUDMaster.Instance.abilitySwapping = false;
  197.  
  198.     }
  199.    
  200.  
  201.  
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement