Skip to content

Instantly share code, notes, and snippets.

@birdinforest
birdinforest / Unity_MVC_Concept.markdown
Last active June 5, 2016 09:22
Concept of Model, View, Controller of MVC pattern in Unity.

Credit

Example and Framework NODE: Use a customized notify system to send event and data by views to controller.

#Models

  • Hold the application’s core data and state, such as player health or gun ammo.
  • Serialize, deserialize, and/or convert between types.
  • Load/save data (locally or on the web).
  • Notify Controllers of the progress of operations.
@birdinforest
birdinforest / GameController.cs
Created May 22, 2016 03:23
Scene manager. State manager. Switch scenes by delegate array. No memory allocation in runtime.
//#define LOG_TRACE_INFO
//#define LOG_EXTRA_INFO
using UnityEngine;
using System.Collections;
//------------------------------------------------------------------------------
// class definition
//------------------------------------------------------------------------------
public class GameController : MonoBehaviour
@birdinforest
birdinforest / ExtentionMethods.cs
Last active July 19, 2016 01:40
# Bounds extension methods: 1. Conversion between local bounds and world bounds; 2. Get corners of bounds. # Vector3 extension methods.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public static class ExtensionMethods {
//---------------------
// transform
//---------------------
@birdinforest
birdinforest / GlobalDefineEditor.cs
Created May 2, 2016 02:34
Unity3D. Change global define by code.
private static void AddOneGlobalDefine(BuildTargetGroup group, string newDefine) {
string define = PlayerSettings.GetScriptingDefineSymbolsForGroup(group);
if(define == string.Empty) {
define = newDefine;
} else {
define = define + ";" + newDefine;
}
PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android, define);
Debug.Log("Current global define: " + define);
}
@birdinforest
birdinforest / EditorWindowIntField.cs
Last active October 9, 2016 23:44
Pop up a editor window with int input field. Example: Create a menu item on editor, when user select that option, pop up a window with int field. Unlock level based on user's input.
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
class EditorWindowIntField : EditorWindow {
private static int input = 1;
private static string _title = string.Empty;
private static string _btnName = string.Empty;
private static System.Action<int> _onBtnClick;
@birdinforest
birdinforest / SensitiveWordCheck.cs
Created April 19, 2016 07:52
Sensitive words check.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public class SensitiveWordCheck : MonoBehaviour {
#if CHS
private const string FILENAME_SENSITIVE_DB = "SensitiveWordsDatabaseCHS";
#else
@birdinforest
birdinforest / DateTimeToStringConvert.cs
Last active April 19, 2016 07:35
+ Convert DateTime to a formatted string and get DateTime by a formatted string. +Strip non-ASCII characters
using System;
using System.Globalization;
using System.Text.RegularExpressions;
public class Test
{
public static void Main()
{
// NOTE: This string contains non-ASCII characters
string dateTimeString = "21‎-‎10‎-‎2014‎ ‎15‎:‎40‎:‎30";
@birdinforest
birdinforest / LookAtPoint.cs
Created March 18, 2016 01:52
Customise Inspector GUI and Scene GUI
//C# Example (LookAtPoint.cs)
using UnityEngine;
[ExecuteInEditMode] // Can execute in edit mode even game is not running.
public class LookAtPoint : MonoBehaviour
{
public Vector3 lookAtPoint = Vector3.zero;
void Update()
{
transform.LookAt(lookAtPoint);
@birdinforest
birdinforest / CreatePolygonMesh.cs
Last active November 18, 2022 16:04
Fill a polygon collider by mesh renderer. Or split a polygon to triangles
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(PolygonCollider2D))]
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class ColliderToMesh : MonoBehaviour
{
void Start()
{
@birdinforest
birdinforest / BitmaskOpration.cs
Last active October 12, 2016 23:39
Tag:Untiy3D, LayerMask, Bitshift, Ignored layer, Bitmask
// Turn on the bit using an OR operation:
private void Show() {
camera.cullingMask |= 1 << LayerMask.NameToLayer("SomeLayer");
}
// Turn off the bit using an AND operation with the complement of the shifted int:
private void Hide() {
camera.cullingMask &= ~(1 << LayerMask.NameToLayer("SomeLayer"));
}