Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
Size: Mime:
using System;
using Fluctio.FluctioSim.Common.Configuration;
using Fluctio.FluctioSim.Core.Components.Prefabs.HeightField.Base;
using JetBrains.Annotations;
using UnityEngine;

#if UNITY_EDITOR
using UnityEditor;
#endif

namespace Fluctio.FluctioSim.Core.Components.Prefabs.HeightField.TextureGround
{
	[AddComponentMenu(Config.PrefixedName+"/Physics/Terrain Layers/Height Texture", Config.ComponentMenuOrder + 40)]
	public class TextureLayer: RemappedLayer
	{
		[field: SerializeField] [CanBeNull] protected Texture2D Texture { get; private set; }

		public override bool IncludeLayer() => base.IncludeLayer() && Texture != null;

		#if UNITY_EDITOR
		public override void OnSelfChanged()
		{
			base.OnSelfChanged();
			CheckImportSettings();
		}

		private void CheckImportSettings()
		{
			if (Texture == null || Texture.isReadable)
			{
				return;
			}
			
			var path = AssetDatabase.GetAssetPath(Texture);
			var importer = AssetImporter.GetAtPath(path) as TextureImporter;
			if (importer != null)
			{
				importer.isReadable = true;
				importer.SaveAndReimport();
				Debug.LogWarning($"Texture at '{path}' was made readable in Texture Import Settings");
			}
			else
			{
				Texture = null;
				Debug.LogError($"Texture at '{path}' is not readable. Please, change it in Texture Import Settings before assigning.");
			}
		}
		#endif

		protected override float GetHeightAtRemapped(Vector2 position)
		{
			if (Texture is null)
			{
				throw new ArgumentNullException(nameof(Texture));
			}
			
			return Texture.GetPixelBilinear(position.x, position.y).grayscale;
		}
	}
}