Search Unity

Question Best way to add outlines to objects at runtime

Discussion in 'Scripting' started by angeldevelopment, Jan 4, 2024.

  1. angeldevelopment


    Sep 28, 2022
    So currently I have an outline shader/material, and when the user looks at something they can interact with, I run this code:
    Code (CSharp):
    1.         originalMatArray = ren.sharedMaterials;
    2.         int originalLen = originalMatArray.Length;
    3.         var outlineMatArray = new Material[originalLen + 1];
    4.         originalMatArray.CopyTo(outlineMatArray, 0);
    5.         outlineMatArray[originalLen] = outlineMaterial;
    6.         ren.materials = outlineMatArray;
    This essential just gets the materials, and add the outline as to be the last material.
    I have my own Interact system, where each object has an Interactable script, and so to get this to work I added a public Renderer property, which specifies the renderer to use, to get the material list. (Thats the ren im using in the code)

    One one hand this gives me the ability to outline any part of the object I want, and the renderer is already cached...on the other hand, I have to go through my entire game and assign a renderer for each object.

    Im wondering if there is a better way to go about doing this, a cleaner way, possibly using textures? I do not want to have to alter all of my objects.