Skip to content

Added Materials as possible targets; fixed useless warnings #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 63 additions & 8 deletions Davinci/Assets/Davinci/Scripts/Davinci.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ private enum RendererType
none,
uiImage,
renderer,
material,
sprite
}

private RendererType rendererType = RendererType.none;
private GameObject targetObj;
private Material targetMaterial;
private string url = null;

private Texture2D loadingPlaceholder, errorPlaceholder;
Expand All @@ -54,8 +56,6 @@ private static Dictionary<string, Davinci> underProcessDavincies
private string uniqueHash;
private int progress;

private bool success = false;

static string filePath = Application.persistentDataPath + "/" +
"davinci" + "/";

Expand Down Expand Up @@ -126,6 +126,21 @@ public Davinci into(Renderer renderer)
return this;
}

/// <summary>
/// Set target Renderer component.
/// </summary>
/// <param name="material">target material component</param>
/// <returns></returns>
public Davinci into(Material material)
{
if (enableLog)
Debug.Log("[Davinci] Target as Material set : " + material);

rendererType = RendererType.material;
this.targetMaterial = material;
return this;
}

public Davinci into(SpriteRenderer spriteRenderer)
{
if (enableLog)
Expand Down Expand Up @@ -275,11 +290,11 @@ public void start()
}
catch (Exception ex)
{
error("Url is not correct.");
error($"Url is not correct: {ex.Message}");
return;
}

if (rendererType == RendererType.none || targetObj == null)
if (rendererType == RendererType.none || (targetObj == null && targetMaterial == null))
{
error("Target has not been set. Use 'into' function to set target component.");
return;
Expand Down Expand Up @@ -411,6 +426,11 @@ private void SetLoadingImage()
renderer.material.mainTexture = loadingPlaceholder;
break;

case RendererType.material:
Material material = targetMaterial;
material.mainTexture = loadingPlaceholder;
break;

case RendererType.uiImage:
Image image = targetObj.GetComponent<Image>();
Sprite sprite = Sprite.Create(loadingPlaceholder,
Expand Down Expand Up @@ -448,13 +468,13 @@ private IEnumerator ImageLoader(Texture2D texture = null)

Color color;

if (targetObj != null)
if (targetObj != null || targetMaterial != null) {
switch (rendererType)
{
case RendererType.renderer:
Renderer renderer = targetObj.GetComponent<Renderer>();

if (renderer == null || renderer.material == null)
if (renderer == null || renderer.materials[1] == null)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a use-case specific change, and doesn't correspond with the use of renderer.material in the rest of the code below.

break;

renderer.material.mainTexture = texture;
Expand Down Expand Up @@ -482,6 +502,42 @@ private IEnumerator ImageLoader(Texture2D texture = null)

break;

case RendererType.material:
Material material = targetMaterial;

if (material == null)
break;

material.mainTexture = texture;
float maxAlphaTex;

if (material.HasProperty("_Color"))
{
material.SetColor("_BaseColor", Color.white);
}

if (fadeTime > 0 && material.HasProperty("_Color"))
{
color = material.color;
maxAlphaTex = color.a;

color.a = 0;

material.color = color;
float time = Time.time;
while (color.a < maxAlphaTex)
{
color.a = Mathf.Lerp(0, maxAlphaTex, (Time.time - time) / fadeTime);

if (material != null)
material.color = color;

yield return null;
}
}

break;

case RendererType.uiImage:
Image image = targetObj.GetComponent<Image>();

Expand Down Expand Up @@ -542,14 +598,14 @@ private IEnumerator ImageLoader(Texture2D texture = null)
}
break;
}
}

if (OnLoadedAction != null)
OnLoadedAction.Invoke();

if (enableLog)
Debug.Log("[Davinci] Image has been loaded.");

success = true;
finish();
}

Expand All @@ -573,7 +629,6 @@ public static string CreateMD5(string input)

private void error(string message)
{
success = false;

if (enableLog)
Debug.LogError("[Davinci] Error : " + message);
Expand Down