- Home /
 
The question is answered, right answer was accepted
Set Material Texture Border
So, I was looking through the Unity GUI system and noticed there's a pixel border you can set of the image that isn't scaled with the content inside it. Is there any way to do this with textures on say a scaled cube? I have found outline shaders but they only display the furthest edges, which in my case will be mostly hidden.
Answer by ritefall · Sep 13, 2015 at 02:02 PM
Came up with this, it gets the job done on cubes well enough
    using UnityEngine;
     using System.Collections;
     
     public class addTextureBorderByScale : MonoBehaviour {
         public Texture2D t;
         public int x;
         public int y;
         public bool useTransformScale = true;
         public enum scaleTypes {XY,XZ,YZ};
         public scaleTypes scaleAxis = scaleTypes.XZ;
         public int scaleMod = 2;
         // Use this for initialization
         void Start () {
         if(useTransformScale) { 
                 switch(scaleAxis) {
                 default:
                 case scaleTypes.XY:
                     x = (int)transform.localScale.x;
                     y = (int)transform.localScale.y;
                     break;
     
                 case scaleTypes.XZ:
                     x = (int)transform.localScale.x;
                     y = (int)transform.localScale.z;
                     break;
     
                 case scaleTypes.YZ:
                     x = (int)transform.localScale.y;
                     y = (int)transform.localScale.z;
                     break;
     
                 }
             }
     
             x = x*scaleMod;
             y = y*scaleMod;
     
             t = new Texture2D(x,y);
             t.filterMode = FilterMode.Point;
     
             for(int i = 0; i < y; i++) {
                 t.SetPixel(0,i,Color.clear);
                 t.SetPixel(x-1,i,Color.clear);
             }
             
     
             for(int i = 0; i < x; i++) {
                 t.SetPixel(i,0,Color.clear);
                 t.SetPixel(i,y-1,Color.clear);
             }
     
             t.Apply();
             //TextureScale.Point(t,1000,1000);
             GetComponent<Renderer>().material.EnableKeyword("_EMISSION");
             GetComponent<Renderer>().material.SetTexture("_EmissionMap",t);
         }
         
         // Update is called once per frame
         void Update () {
         
         }
     }
     
     
     
 
              Follow this Question
Related Questions
alpha mask on a 0 Answers
Changing HDRP Material's Texture At Runtime Not Working? 2 Answers
How to apply a color to the second texture in this shader? 1 Answer
Multiple Shaders vs. Single Texture - Please help me overthink this. 2 Answers
How can I have a transparent ring around a transparent planet? 0 Answers