Assigning textures in project panel to materials dynamically or via editor scripts

Is it possible to assign textures in project panel to materials via editor scripts, or at runtime?

1 Answer

1

Correct me if I misunderstood the question, but I think this may be what you’re trying to achieve?

    public Texture colorMap;//Assign this in inspector
    public Texture colorMap2;//Assign this in inspector
    
//The next lines will assign the above textures to the gameobject's material texture:
    if (condition)
    {
    gameObject.renderer.material.mainTexture = colorMap;
    }
    else if (!condition)
    {
    gameObject.renderer.material.mainTexture = colorMap2;
    
    }

Depending on the shader you may have access to other textures as well through scripting, such as normal maps, etc.

this would work if you can reference your material, but i am not sure how to reference the material if it is just in the project pane (not yet in hierarchy)

–