Various Scripts and Application Code Segments for .NET, VB, C#, C++, C, Java, JavaScript, HTML, Python, Perl, AutoIT, Batch, ASP Classic, Objective-C, Swift, Unreal Engine 4, Unity3D & others. Also contains numerous IT tidbits, procedures, and tricks including Technology Hacks on various platforms.
Often when you use the same apk for google play and amazon stores and you need to determine at run time whether your device is running Android OS or a Kindle on Fire OS. You may need to even further determine if the device is an Amazon Fire TV or Android TV app.
In Unity3D you can often use SystemInfo.deviceModel, and put it into if/then or case and set appropriate flags. Then you can implement google play, game circle, forward to the appropriate store for rating an app, etc.:
public static string getDevice() {
if ( SystemInfo.deviceModel.ToLower().Contains("aftb")) {
isAmazon = true;
return "Fire TV";
}
if ( SystemInfo.deviceModel.ToLower().Contains("amazon")) {
In Unity3D or any game engine you sometimes need music to play across multiple scenes. To accomplish this in Unity3D you need to create a MusicController GameObject that doesn't destroy on load. Below I do this with a Singleton Class that retains from scene to scene! Game.music is a static boolean field that tells rest of the system that music is enabled. On this controller I have an array of AudioClip[]. which I can have songs in ogg or mp3 format. using UnityEngine; using System.Collections; using System; using System.IO; using Prime31; public class MusicController : MonoBehaviour { #region Music public static MusicController Instance { get; private set; } public AudioClip[] Tracks; int currentTrack=0; public void Awake() { if (!Game.music) { Instance = null; Destroy(gameObject); return; } if (Instance!=null) { Destroy(gameObject); GetComponent<AudioSource>().Stop(); } if (Instance==null) { Instance = this; DontDestroyOnLoad(this.gameObject); } } public string trackName() { string x = ""; if (GetComponent<AudioSource>().clip!=null) x = GetComponent<AudioSource>().clip.name; return x; } public void playRandomTrack(bool t) { GetComponent<AudioSource>().Stop(); GetComponent<AudioSource>().enabled = true; int x = UnityEngine.Random.Range(0,Tracks.Length); GetComponent<AudioSource>().clip = Tracks[x]; GetComponent<AudioSource>().Play(); currentTrack = x; if (GUI.Instance!=null) GUI.Instance.ShowTextToast("Track: " + trackName(),5f); } public void playRandomTrack() { playRandomTrack(true); } public void Start() { if (Game.music) { Instance = null; playRandomTrack(); Awake(); return; } } public void Update() { if (!Game.music) { Instance = null; Destroy(gameObject); return; } if (!GetComponent<AudioSource>().isPlaying && GetComponent<AudioSource>().clip!=null) playRandomTrack(); if (Game.gameOver) { GetComponent<AudioSource>().volume = 0.75f; } else if (Game.shouldBegin) { GetComponent<AudioSource>().volume = 0.25f; } else GetComponent<AudioSource>().volume = 0.75f; } #endregion }
Can't tell you how often I use observers, and delegates and events in C# when using Unity3D. Say you have a boolean value called isWinner in your Game Class. If you want to see if that bool value has became true in a different class you need to subscribe using an observer and observable. Since isWinner is boolean, a different class called Leaderboards needs to run a method that increments a GameCenter leaderboard. You accomplish this through the Observer Pattern and Delegates. In a Game class: private static bool _isWinner = false; public static bool isWinner { get { return _isWinner; } set { _isWinner = value; if (_isWinner) { initiateWinner(); } } } public delegate void Winner(); public static event Winner initiateWinner; public static void finishGame() { isWinner = true; } In a Leaderboard class: void OnEnable() { Game.initiateWinner += doWin; } void OnDisable() { Game.initiateWinner -= doWin; } void doWin() { // do your gamecenter, social, prime31 code here } From above it's easy to assess that is happening. isWinner is changed, and the event initateWinner() is initiated. Then on the Leaderboard class it's observed and calls doWin();.
To change the resolution in Unity 5.3.1f1 is difficult in iOS when it comes to granularity of the device. Prior to 5.3.x, you could simply put in different modes for resolution. Auto Performance, Native Resolution, and Auto Quality were some of the few. With the new version you can not adjust this so it requires that your hands get a bit dirty. See below in DisplayManager.mm. look for UnityScreenScaleFactor: Modify as below:
// On AppleTV screen.nativeScale returns NaN when device is in sleep mode
if (isnan(screen.nativeScale))
return1.0f;
else
return screen.nativeScale * resMult;
}
return screen.scale * resMult;
}
As you can see above, the resMult is what you multiply the resolution accordingly. For example if you take a 0.5f multiplyer on an iPad Retina (2048x1536) it'll turn your resolution to 1/2 or 0.5 of it which is essentially 1024x768.
Why you may ask would you want to do this? Speed, simply. Some games run entirely to slow in full resolution with Unity3D. It is optimal to use a multiplyer and scale down for older devices with AA or AAA games graphics.
Singleton's are bad design patterns because they are essentially global variables or static classes vs individually static variables. Though we are taught to stay away from them in gaming they can come in handy, especially in Unity. I usually make my GameControllers singleton because there is essentially no chance that I'd need more than once instance of it.
Lazy initialization is nice because it'll initialize your instance when you need it, as you need it in the lifetime of your class.
As follows:
using UnityEngine; using System.Collections; using System; using System.IO; using Prime31; public class GameController : Game { public static GameController Instance { get; private set; } #region Unity Behaviour public void Awake () { Instance = this; } #endregion
}
You can design the class to inherit attributes (as mine does above from the Abstract Game Class). To reference the class from external classes you'd use GameController.Instance.*. To access the internal variables when you use this Singleton you'll have to declare them as public. In lieu of making public variables it's Ideally best to make them private and use getters and setters.
You can also pass Singleton's into methods and functions if you say for arguments sake have 2 classes inheriting from Game, both Singletons. You can decide which Singleton you want to pass in runtime, granted that both classes have the same method "someMethod()" in them publically. This can be done in runtime as follows:
public void callMethod(Game p) { p.someMethod(); } public void test() { callMethod(GameController.Instance); }
Though many argue Singleton is arguably an Anti-pattern, you can make the counter argument because of the usefulness and ubiquity of rampant use, it's legitimate in any programmer's arsenal.
Making the Camera shake in Unity3D can add much needed dramatic effects to your game. To implement you need to manipulate position and rotation in random ranges. See below:
var originPosition:Vector3; var originRotation:Quaternion; var shake_decay: float; var shake_intensity: float; function ShakeCustom(x:float, y:float){ originPosition = transform.position; originRotation = transform.rotation; shake_intensity = x; shake_decay = y; } function LateUpdate() { if(shake_intensity > 0){ transform.position = originPosition + Random.insideUnitSphere * shake_intensity; transform.rotation = Quaternion( originRotation.x + Random.Range(-shake_intensity,shake_intensity)*.2, originRotation.y + Random.Range(-shake_intensity,shake_intensity)*.2, originRotation.z + Random.Range(-shake_intensity,shake_intensity)*.2, originRotation.w + Random.Range(-shake_intensity,shake_intensity)*.2); shake_intensity -= shake_decay; } }
In this script which you attach to the Enemy Objects, you may find the nearest object Tagged "Player". The Enemy faces the Player, and chases within a proximity, with a given frequency. See below:
/// <summary> /// AI character controller. /// Just A basic AI Character controller /// will looking for a Target and moving to and Attacking /// </summary>
How do you change the Pixel Error, Base Map Distance, Cast Shadows, Tree Distance and other settings from runtime, not in inspector using UnityScript or C#? It's relatively easy when you reference the Component on Terrain. This script should work:
This simple script tells the Game/App from Unity to shake/vibrate the Device if iOS/Android. If the device doesn't support vibrate, it'll simply do nothing!:
With this simple coding you can play a playlist on your device from your Unity3D Game. Why license royalties when you can get the user to do it for you per individual basis. I am using this in my game Snowboarding+ (see gameplay demo above). If there is no music on your device it'll finally play the local music in your Unity 3D app. This plays the playlist called Snowboarding+
in AppController.mm (also add MediaPlayer.framework):
This segment is for simple movement for a player in an Air Hockey Game on a 3D plane. You move mouse x and y and the y corresponds on the z plane. This is from my Air Hockey 3D game available currently in the Android, iOS and Mac App Stores.
var other : GameObject ; var dist : float; var hitSound : AudioClip; var spark : Transform; var offset : Vector2 = Vector2.zero; function spark1() { Instantiate (spark, transform.position, transform.rotation); yield WaitForSeconds(0.5); Destroy(GameObject.Find("Sparks(clone)")); } function Update () { if (Time.timeScale != 0) { other = GameObject.Find ("Sphere"); var Menu = GameObject.Find("Menu"); var script : menu = Menu.GetComponent(menu); var hit : RaycastHit; var up = Vector3 ((transform.position.x - other.transform.position.x) * -1 , 0, (transform.position.z - other.transform.position.z) * -1 );
This simple script is how to submit scores and points to the Game Center from a Unity3D Game. People use the prime31 plugin, and pay the extra money, which dumbfounds me. Just do it yourself! I've done both JS and C# implementations of the same code... Here it goes in JavaScript:
#if UNITY_IPHONE import UnityEngine.SocialPlatforms; #endif //These next two methods show the leaderboard
function DoLeaderboard () { #if UNITY_IPHONE Social.localUser.Authenticate (ProcessAuthentication); #endif } function ProcessAuthentication (success: boolean) { #if UNITY_IPHONE if (success) { Debug.Log ("Authentication successful"); Social.CreateLeaderboard(); Social.CreateLeaderboard().id = "YourLeaderBoardID"; Social.ShowLeaderboardUI(); } else Debug.Log ("Failed to authenticate"); #endif }
//these next two methods report a score to the leaderboard
function reportScoreToBoard() { #if UNITY_IPHONE Social.localUser.Authenticate (ReportScore); #endif }
function ReportScore (success: boolean) { #if UNITY_IPHONE if (success) { Debug.Log ("Authentication successful"); Social.CreateLeaderboard(); Social.CreateLeaderboard().id = "YourLeaderBoardID"; Social.ReportScore(PlayerPrefs.GetInt("timeElapsed"),"YourLeaderBoardID", function(result) { if (result) Debug.Log ("Successfully reported timeElapsed, Virgin:" + PlayerPrefs.GetInt("timeElapsed")); else Debug.Log ("Failed to report timeElapsed");}); //if you want uncomment below to show leaderboard! //Social.ShowLeaderboardUI(); } else Debug.Log ("Failed to authenticate"); #endif }