Skip to content

Instantly share code, notes, and snippets.

@hdf
Created May 18, 2015 20:39
Show Gist options
  • Save hdf/4ed8814e4806099db8de to your computer and use it in GitHub Desktop.
Save hdf/4ed8814e4806099db8de to your computer and use it in GitHub Desktop.
Unity Desktop Test
using UnityEngine;
using System.Collections;
public class CubeMaker : MonoBehaviour {
GameObject c, p;
public static ReflectionProbe Probe1;
// Use this for initialization
void Start() {
c = GameObject.CreatePrimitive(PrimitiveType.Cube);
p = GameObject.CreatePrimitive(PrimitiveType.Plane);
p.GetComponent<Renderer>().material =
Resources.Load<Material>("CubeMat");
//Shader.Find("Nature/Terrain/Diffuse"));
c.GetComponent<Renderer>().material = p.GetComponent<Renderer>().material;
//Shader.Find("Legacy Shaders/Transparent/Diffuse"));
c.SetActive(false);
p.SetActive(false);
GameObject[] tmpList = GameObject.FindObjectsOfType<GameObject>();
int n = tmpList.Length;
for (int i = 0; i < n; i++) {
Probe1 = tmpList[i].GetComponent<ReflectionProbe>();
if (Probe1 != null)
break;
}
Probe1.RenderProbe();
}
Color highlightColor = Color.red;
private Mesh lastMesh;
GameObject lastPlane;
// Update is called once per frame
void Update() {
if (Input.GetKey(KeyCode.LeftControl))
return;
RaycastHit hit;
if (!Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),
out hit, Mathf.Infinity, ~(1 << 8))) {
RemoveHighlights();
return;
}
if (Input.GetButtonDown("Fire1")) {
putCube(hit);
return;
}
if (Input.GetButtonDown("Fire2")) {
if (hit.collider.name == "HDF")
Destroy(hit.collider.gameObject);
if (hit.collider.name == "Plane(Clone)")
Destroy(hit.collider.gameObject.transform.parent.gameObject);
return;
}
if (hit.collider.name == "HDF") {
HighlightSelectedFace(hit);
} else if (hit.collider.name != "Plane(Clone)") {
if (lastPlane != null) {
Destroy(lastPlane);
lastPlane = null;
}
RemoveHighlights();
}
}
void putCube(RaycastHit hit) {
Vector3 baseloc = (hit.collider.name == "HDF") ?
hit.transform.position + hit.normal : hit.point + hit.normal * 0.5f;
GameObject cube = (GameObject)Instantiate(c, baseloc,
hit.transform.rotation);
cube.GetComponent<Collider>().name = "HDF";
cube.SetActive(true);
//cube.AddComponent(Rigidbody);
/*/ Add second smaller cube
GameObject cube2 = (GameObject)Instantiate(cube);
cube2.transform.localScale = new Vector3(0.95f, 0.95f, 0.95f);
cube2.SetActive(true);*/
}
void putPlane(RaycastHit hit) {
if (lastPlane != null && hit.collider.gameObject.transform.childCount > 0 &&
lastPlane.transform.position == hit.transform.position +
hit.normal * 0.51f)
return;
if (lastPlane != null) {
Destroy(lastPlane);
lastPlane = null;
}
lastPlane = (GameObject)Instantiate(p, hit.transform.position + hit.normal *
0.51f, hit.transform.rotation);
lastPlane.transform.parent = hit.collider.gameObject.transform;
lastPlane.GetComponent<Renderer>().material.color =
new Color(0, 1, 0, 0.1f);
lastPlane.transform.rotation = Quaternion.FromToRotation(
lastPlane.transform.up, hit.normal) * lastPlane.transform.rotation;
lastPlane.transform.localScale *= 0.11f;
lastPlane.layer = 8;
lastPlane.SetActive(true);
}
void HighlightSelectedFace(RaycastHit hit) {
if (hit.collider.name == "HDF")
putPlane(hit);
Mesh mesh = hit.collider.gameObject.GetComponent<MeshFilter>().mesh;
if (lastMesh != null && lastMesh != mesh)
RemoveHighlights();
Vector3 normal = hit.transform.InverseTransformDirection(hit.normal);
Vector3[] normals = mesh.normals;
int n = mesh.vertices.Length;
Color[] colors = new Color[n];
for (int i = 0; i < n; i++) {
if (normals[i] == normal) {
colors[i] = highlightColor;
} else {
colors[i] = Color.white;
}
}
mesh.colors = colors;
lastMesh = mesh;
}
void RemoveHighlights() {
if (lastMesh == null)
return;
int n = lastMesh.vertices.Length;
var colors = new Color[n];
for (int i = 0; i < n; i++) {
colors[i] = Color.gray;
}
lastMesh.colors = colors;
lastMesh = null;
}
}
using UnityEngine;
using System.Collections;
public class FPSMeter : MonoBehaviour {
float deltaTime = 0.0f;
// Use this for initialization
void Start() {
}
// Update is called once per frame
void Update() {
deltaTime += (Time.deltaTime - deltaTime) * 0.1f;
}
void OnGUI() {
int w = Screen.width, h = Screen.height;
GUIStyle style = new GUIStyle();
Rect rect = new Rect(0, 0, w, h * 2 / 100);
style.alignment = TextAnchor.UpperRight;
style.fontSize = h * 2 / 100;
style.padding.right = 5;
style.normal.textColor = new Color(0.0f, 0.0f, 0.0f, 1.0f);
float msec = deltaTime * 1000.0f;
float fps = 1.0f / deltaTime;
GameObject[] cubes = GameObject.FindObjectsOfType<GameObject>()
as GameObject[];
int hdfs = 0;
int n = (cubes != null) ? cubes.Length : 0;
for (int i = 0; i < n; i++)
if (cubes [i].GetComponent<Collider>() != null &&
cubes [i].GetComponent<Collider>().name == "HDF")
hdfs++;
string text = string.Format("{0:0.0} ms ({1:0.} fps)\nBoxes: {2}",
msec, fps, hdfs);
GUI.Label(rect, text, style);
}
}
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class Pane1 : MonoBehaviour {
// Use this for initialization
void Start() {
GetComponent<Renderer>().sharedMaterial.shader =
Shader.Find("Unlit/Texture");
}
// Update is called once per frame
void Update() {
}
void OnMouseDown() {
if (!Input.GetKey(KeyCode.LeftControl))
return;
UnityStandardAssets.ImageEffects.Grayscale s =
Camera.main.GetComponent("Grayscale") as
UnityStandardAssets.ImageEffects.Grayscale;
s.enabled = !s.enabled;
}
}
using UnityEngine;
using System.Collections;
public class Scene : MonoBehaviour {
// Use this for initialization
void Start() {
#if !UNITY_EDITOR
SceneHelper.centerScreen();
#endif
GameObject p = SceneHelper.setPanelStuff();
#region Get current wallpaper path
string pathWallpaper = "";
Microsoft.Win32.RegistryKey regKey =
Microsoft.Win32.Registry.CurrentUser.OpenSubKey(
"Control Panel\\Desktop", false);
if (regKey == null)
return;
pathWallpaper = regKey.GetValue("WallPaper").ToString();
regKey.Close();
#endregion
#region Assign wallpaper to Panel
byte[] fileData = System.IO.File.ReadAllBytes(pathWallpaper);
Texture2D tex = new Texture2D(2, 2);
tex.LoadImage(fileData);
p.GetComponent<Renderer>().material.mainTexture = tex;
#endregion
}
// Update is called once per frame
void Update() {
SceneHelper.handleResize();
}
void OnApplicationQuit() {
PlayerPrefs.DeleteAll();
}
}
using UnityEngine;
using System;
using System.Collections;
using System.Runtime.InteropServices;
public static class SceneHelper {
#region Pinvoke stuff
[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter,
int X, int Y, int cx, int cy, uint uFlags);
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);
static class SWP {
public static readonly uint
NOSIZE = 0x0001,
NOMOVE = 0x0002,
NOZORDER = 0x0004,
NOREDRAW = 0x0008,
NOACTIVATE = 0x0010,
DRAWFRAME = 0x0020,
FRAMECHANGED = 0x0020,
SHOWWINDOW = 0x0040,
HIDEWINDOW = 0x0080,
NOCOPYBITS = 0x0100,
NOOWNERZORDER = 0x0200,
NOREPOSITION = 0x0200,
NOSENDCHANGING = 0x0400,
DEFERERASE = 0x2000,
ASYNCWINDOWPOS = 0x4000;
}
[StructLayout(LayoutKind.Sequential)]
struct RECT {
public int Left;
public int Top;
public int Right;
public int Bottom;
}
#endregion
static bool wasFullscreen = false;
static float lastAspect;
// Set Panel distance and aspect ratio
public static GameObject setPanelStuff(string panelName = "Plane1") {
GameObject p = GameObject.Find(panelName);
p.transform.localScale = new Vector3(Camera.main.aspect, 1, 1);
p.transform.position = new Vector3(
-p.transform.position.y /
Mathf.Tan(Camera.main.fieldOfView / 2 * Mathf.PI / 180),// * 1.25f,
p.transform.position.y, p.transform.position.z);
lastAspect = Camera.main.aspect;
return p;
}
public static void centerScreen() {
if (Screen.fullScreen)
return;
IntPtr taskbarWnd = FindWindow("Shell_TrayWnd", "");
RECT taskbarRect;
GetWindowRect(taskbarWnd, out taskbarRect);
IntPtr hWnd = GetForegroundWindow();
SetWindowPos(hWnd, 0,
Screen.currentResolution.width / 2 - Screen.width / 2,
Screen.currentResolution.height / 2 - Screen.height / 2 -
(taskbarRect.Bottom - taskbarRect.Top) / 2,
0, 0, SWP.NOSIZE | SWP.NOZORDER);// | SWP.FRAMECHANGED);
}
public static void handleResize() {
if (!Screen.fullScreen) {
if (wasFullscreen) {
Screen.SetResolution(1024, 768, false);
wasFullscreen = false;
}
} else
wasFullscreen = true;
if (lastAspect != Camera.main.aspect) {
setPanelStuff();
CubeMaker.Probe1.RenderProbe();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment