I haven't updated it for a long time. I have finally finished some urgent matters. I will update my blog!
Not much nonsense. Let's talk about it later. Let's talk about it today.
We have discussed how to create an image and use code to draw a color block. Today we will explain how to draw an image in the scenario.
First, let's do the homework first.
In unity, there are many image types. The default value is texture, and there are other attributes.
Some people may encounter the reason why the materials I put will become blurred after compilation. This is because you have not modified the image attributes in unity.
In texture mode,
Let's analyze the attributes of this image,
The "Wrap mode" mode is used continuously when an image is selected. It can also reduce the impact of white edges on the image.
The filter mode is the file mode of the image. To put it bluntly, it is a dot, two dots, and a triangle .. The higher the image quality, the higher the memory.
Anise level: I haven't used this yet. It seems that it is used as a bitmap.
Max size supports the maximum number of images (note that it is the largest. If my figure is 100*100, you can choose 1024. It doesn't matter if you don't waste memory, this is the largest gallery support. If your image exceeds 1024, the value will also change .)
Format is the compression mode, which determines the most important part of the memory. There are three options: automatic compression (the most economical) and 16-bit compression (which can be said in the past ), the real color can be said to have no compression, and the memory size increases accordingly while ensuring the quality.
Advanced option mode: The most common mode.
Non power of 2: If you don't want your graph to become a square that matches the Npower of 2, select none to ensure the rationality of the graph size. Otherwise, select automatic adaptation, maximum adaptation, and minimum adaptation.
Cubemap: usually not used. This is the texture used on the model.
Read/W: whether read/write is supported. We recommend that you select this option.
For more information, see the document.
Format: Generally, we use a 32-bit format. You can also select a 16-bit format. Reduce memory.
Finally, I will send a sentence. Try to build a large image and compress the UV image (you can also use ngui to automatically compress the image, or compress it yourself. I will talk about how to create your own gallery later ), it is recommended that the size of a large image be less than 1024. Reduce the number of faces as much as possible and merge the faces as much as possible. Objective To reduce memory and drawcall.
Okay. Next step. We started to render the image into the scenario.
As mentioned above, we need to draw a piece. Below, we need two parts.
1. Replace the shader. The previous one is the solid color shader. We will use the shader with textures below.
2. We used a fixed width and height, because the image size is not fixed, so we need to adapt the proportion.
Code:
Shader:
Shader "VK/VKTextureShader" { Properties { _Color ("Main Color", COLOR) = (1,1,1,1) _MainTex ("Base (RGB) Trans (A)", 2D) =""{} } SubShader { Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"} Pass { Blend SrcAlpha OneMinusSrcAlpha Cull off SetTexture [_MainTex] { constantColor [_Color] Combine texture *constant } } }}
Call the public method class initbase of the front part to inherit the vkview. The front part already exists. Just copy it.
Using unityengine; using system. collections; public class vkimageview: vkview {material mesh; mesh imgviewdefultmesh; [hideininspector] public texture imgviewtex, highlightedtex; [hideininspector] public float scale = 1; [hideininspector] Public String info = NULL; [hideininspector] public bool highlighted = false; [hideininspector] public float alpha = 1; // use this for initializationvoid start () {Imgviewdefultmat = new material (shader. find ("VK/vktextureshader"); imgviewdefultmesh = new mesh (); getcomponent <meshfilter> (). mesh = imgviewdefultmesh; getcomponent <meshrenderer> (). material = imgviewdefultmat; updateimageview ();} public void updateimageview () {If (imgviewtex! = NULL) {If (! Highlighted) {If (imgviewdefultmat! = NULL) imgviewdefultmat. maintexture = imgviewtex; If (imgviewdefultmesh! = NULL) imgviewdefultmesh. vertices = initbase. initvertice (imgviewtex. width * scale, imgviewtex. height * scale, ancpointx, ancpointy); Height = imgviewtex. height; width = imgviewtex. width;} else {If (imgviewdefultmat! = NULL) imgviewdefultmat. maintexture = highlightedtex; If (imgviewdefultmesh! = NULL) imgviewdefultmesh. vertices = initbase. initvertice (highlightedtex. width * scale, highlightedtex. height * scale, ancpointx, ancpointy); Height = highlightedtex. height; width = highlightedtex. width ;}} else {If (imgviewdefultmat! = NULL) imgviewdefultmat. maintexture = NULL; If (imgviewdefultmesh! = NULL) imgviewdefultmesh. vertices = initbase. initvertice (width * scale, height * scale, ancpointx, ancpointy);} If (imgviewdefultmat! = NULL) {color newcolor = imgviewdefultmat. color; imgviewdefultmat. Color = new color (newcolor. R, newcolor. G, newcolor. B, alpha);} If (imgviewdefultmesh! = NULL) {imgviewdefultmesh. triangles = initbase. inittri (); imgviewdefultmesh. normals = initbase. initnormal (); imgviewdefultmesh. UV = initbase. inituv () ;}} public void switchbutton () {// you can delete it first, which is used by the conversion button. Vkbutton button = gameobject. addcomponent <vkbutton> (); button. buttondefultmesh = imgviewdefultmesh; button. buttondefultmat = imgviewdefultmat; button. buttontex = imgviewtex; button. pressbuttontex = highlightedtex; button.info = Info; button. scale = scale; button. ancpointx = ancpointx; button. ancpointy = ancpointy; button. updatebutton (); destroyimmediate (getcomponent <vkimageview> ());}}
Editor class under Editor
using UnityEngine;using System.Collections;using UnityEditor;[CustomEditor(typeof(VKImageView))]public class VKImageViewEditor : Editor { public override void OnInspectorGUI () { base.OnInspectorGUI (); VKImageView imgView = (VKImageView)target; imgView.imgViewTex = EditorGUILayout.ObjectField ("ImageTexture",imgView.imgViewTex,typeof(Texture),true)as Texture; imgView.highLightedTex = EditorGUILayout.ObjectField ("HighLightedTex",imgView.highLightedTex,typeof(Texture),true)as Texture; imgView.alpha = EditorGUILayout.Slider("Alpha",imgView.alpha,0.0f,1.0f); imgView.highLighted = EditorGUILayout.Toggle ("highLighted",imgView.highLighted); imgView.info = EditorGUILayout.TextField ("info",imgView.info); imgView.ancPointx = EditorGUILayout.Slider("AnchorX",imgView.ancPointx,0.0f,1.0f); imgView.ancPointy = EditorGUILayout.Slider("AnchorY",imgView.ancPointy,0.0f,1.0f); if(imgView.imgViewTex == null){ imgView.width=EditorGUILayout.IntField("Width",imgView.width); imgView.height=EditorGUILayout.IntField("Height",imgView.height); } GUILayout.BeginHorizontal(); if(GUILayout.Button("2X")){ imgView.scale = 0.5f; } if(GUILayout.Button("1X")){ imgView.scale = 1f; } if(GUILayout.Button("1.5X")){ imgView.scale = 0.75f; } GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); if(GUILayout.Button("ChangeName")){ if(imgView.imgViewTex!=null){ imgView.name = imgView.imgViewTex.name; } } if(GUILayout.Button("SwitchButton")){ imgView.switchButton(); } GUILayout.EndHorizontal(); imgView.updateImageView(); if(imgView!=null) EditorUtility.SetDirty (imgView); EditorUtility.UnloadUnusedAssets (); }}
Many. The first part is described here first. Next time, you can leave a message if you don't know it or you can't understand it. Learn, please detour. Thank you.