{
"examples": [
{
"title": "Draw Line",
"description": "This example demonstrates how to draw a line using OpenGL in Unity.",
"code": "[csharp]\npublic class GLTest : MonoBehaviour\n{\n\tpublic Shader shader;\n\tpublic Texture2D t2d;\n\tprivate Material mat;\n\n\tvoid Start()\n\t{\n\t\tmat = new Material(shader);\n\t\tmat.mainTexture = t2d;\n\t}\n\n\tvoid OnPostRender()\n\t{\n\t\tif (!mat)\n\t\t{\n\t\t\tDebug.LogError(\"Please Assign a material on the inspector\");\n\t\t\treturn;\n\t\t}\n\t\tGL.PushMatrix();\n\t\tmat.SetPass(0);\n\t\tGL.LoadOrtho();\n\t\tGL.Begin(GL.QUADS);\n\t\tGL.Vertex3(0, 0, 0.1F);\n\t\tGL.Vertex3(1f, 0, 0.1F);\n\t\tGL.Vertex3(1f, 1, 0.1F);\n\t\tGL.Vertex3(0, 1, 0.1F);\n\t\tGL.End();\n\t\tGL.PopMatrix();\n\t}\n}\n\nShader \"Custom/GLDrawLine\"\n{\n\tProperties {\n\t\t_MainTex (\"Base (RGB)\", 2D) = \"white\" {}\n\t}\n\tSubShader\n\t{\n\t\tPass\n\t\t{\n\t\t\tCull off\n\t\t\tBlend DstAlpha zero\n\t\t\tColor(1,1,1,1)\n\t\t}\n\t}\n}"
},
{
"title": "Take Screenshot",
"description": "This example demonstrates how to take a screenshot of the current frame using Unity's API.",
"code": "[csharp]\nusing System.IO;\nusing UnityEngine;\nusing System.Collections;\n\nclass ScreenShot : MonoBehaviour\n{\n\tvoid Start()\n\t{\n\t\tStartCoroutine(UploadPNG() );\n\t}\n\n\tIEnumerator UploadPNG()\n\t{\n\t\tyield return new WaitForEndOfFrame();\n\t\tprint (\"yuuuuu\");\n\t\tint width = Screen.width;\n\t\tint height = Screen.height;\n\t\tTexture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);\n\t\ttex.ReadPixels(new Rect(0, 0, width, height), 0, 0);\n\t\ttex.Apply();\n\t\tbyte[] bytes = tex.EncodeToPNG();\n\t\tFile.WriteAllBytes(Application.dataPath + \"/ss.png\", bytes);\n\t\tUnityEditor.AssetDatabase.Refresh();\n\t}\n}"
},
{
"title": "Show Alpha",
"description": "This example demonstrates how to render a texture with alpha blending using OpenGL in Unity.",
"code": "[csharp]\nclass GLTest : MonoBehaviour\n{\n\tpublic Shader shader;\n\tpublic Texture2D t2d;\n\tprivate Material mat;\n\n\tvoid Start()\n\t{\n\t\tmat = new Material(shader);\n\t\tmat.mainTexture = t2d;\n\t}\n\n\tvoid OnPostRender()\n\t{\n\t\tif (!mat)\n\t\t{\n\t\t\tDebug.LogError(\"Please Assign a material on the inspector\");\n\t\t\treturn;\n\t\t}\n\t\tGL.PushMatrix();\n\t\tmat.SetPass(0);\n\t\tGL.LoadOrtho();\n\t\tGL.Begin(GL.QUADS);\n\t\tGL.Vertex3(0, 0, 0.1F);\n\t\tGL.Vertex3(1f, 0, 0.1F);\n\t\tGL.Vertex3(1f, 1, 0.1F);\n\t\tGL.Vertex3(0, 1, 0.1F);\n\t\tGL.End();\n\t\tGL.PopMatrix();\n\t}\n}\n\nShader \"Custom/GLDrawLine\"\n{\n\tProperties {\n\t\t_MainTex (\"Base (RGB)\", 2D) = \"white\" {}\n\t}\n\tSubShader\n\t{\n\t\tPass\n\t\t{\n\t\t\tCull off\n\t\t\tBlend DstAlpha zero\n\t\t\tColor(1,1,1,1)\n\t\t}\n\t}\n}"
}
]
}