I'm using the Texture2D.GetData() method to create a Tilemap. This always works the first time I execute it, but the second time the method simply returns other Colors, confusing the whole algorithm. I see absolutely no reason for this.
My code:
switch (KartenID)
{
case 1: WasserKarte = Content.Load<Texture2D>("Karte/Karte1");
break;
case 2: WasserKarte = Content.Load<Texture2D>("Karte/Karte2");
break;
default:WasserKarte = Content.Load<Texture2D>("Karte/StandardKarte");
break;
}
KARTE_BREITE = WasserKarte.Width;
KARTE_HOEHE = WasserKarte.Height;
Color[] c = new Color[KARTE_BREITE* KARTE_HOEHE];
WasserKarte.GetData<Color>(c);
//and so on...
I am really frustated by now. I can't really tell if the Content.Load<> returns a wrong Texture, but I don't think so. Its just the second time I'm running this code without restarting the application.
I would really appreciate help.
Same texture should always return same color data. For example:
var texture = Content.Load<Texture2D>("img");
var clr = new Color[texture.Width*texture.Height];
texture.GetData(clr);
texture = Content.Load<Texture2D>("img");
var clr2 = new Color[texture.Width*texture.Height];
texture.GetData(clr2);
for (int i = 0; i < clr.Length; i++)
{
if (clr[i] != clr2[i])
{
// never gets hit
Console.WriteLine("Error - color {0} != color {1}", clr[i], clr2[i]);
}
}
Since you didn't provide more code I can only assume, that you doing something not intentional to a texture at runtime. However there is a simple way to fix your problem and optimize the code at the same time. Create class for storing textures information, something like this:
class Resource
{
public Texture2D Texture;
public Color[] ColorData;
}
Dictionary<string, Resource> Resources;
Instead of calling Content.Load each time you need to get color information, read all textures that is used in the level, then assign texture identifiers and get color data for each texture only once. Later just reuse already loaded color data from resources:
var colorData = Resource["KartenID"].ColorData;
// do something with color data
Of course this is just very simple example of game resources management, you can write more sophisticated resources manger to meet your needs.
Related
I was watching this tutorial and did complete it, now I want to extend it.
I want to open multiple images using FileExplorer in unity and then able to show the images based on the slider's value as seen in this image:
Any help appreciated.
This doesn't answer the question but what you asked in the comments
Can you give me a little code for "just create a list and have the opened images in it, when the value of the slider changes, just set the Raw Image's sprite to that of the image in the list by using an int to get the image from the list."?
[RequireComponent(typeof(RawImage))]
public class ImageSwitcher : MonoBehaviour
{
private RawImage image;
public Slider SliderComponent;
// Get the textures somehow
public List<Texture>() textures = new List<Texture>();
private void Awake()
{
image = GetComponent<RawImage>();
if(!SliderComponent)
{
Debug.LogError("No SliderComponent referenced!", this);
return;
}
// Make the slider accept only whole numbers
SliderComponent.wholeNumbers = true;
SliderComponent.value = 0;
SliderComponent.minValue = 0;
// Index is 0 based so can maximal be list count -1
SliderComponent.maxValue = textures.Count - 1;
// Register a listener for onValueChanged
// Remove the listener first to avoid multiple listeners added
SliderComponent.onValueChanged.RemoveListener(OnSliderChanged);
SliderComponent.onValueChanged.AddListener(OnSliderChanged);
}
private void OnDestroy ()
{
// Always clean up listeners when not needed anymore
SliderComponent.onValueChanged.RemoveListener(OnSliderChanged);
}
// Use this to change the Texture list
// and Max value of the slider afterwards
public void UpdateSlider(List<Texture> textures)
{
// Update the texture list
this.textures = textures;
// Update the max value of the slider
SliderComponent.maxValue = textures.Count - 1;
// Unity might automatically clamp the slider value
// after the maxValue was changed
// But just to be sure we can do it as well
SliderComponent.value = Mathf.Clamp(SliderComponent.value, 0, textures.Count - 1);
}
// Called when the slider value is changed
private void OnSliderChanged()
{
// Get the value as int
int index = Mathf.RoundToInt(SliderComponent.value);
if(index < 0 || index > textures.Count - 1)
{
// Should actually be impossible but just in case log it
Debug.Log("Slider produced impossible index: " + index, this);
return;
}
// Get according texture from list
var texture = textures[index];
// Set texture
image.texture = texture;
}
}
However this doesn't completely solve your question
Unity3d(Editor): Opening Multiple files using EditorUtility.OpenFilePanel()
as mentioned in this thread it is not possible since EditorUtility.OpenFilePanel returns only one single filepath as string.
So short answer would be: It's (currently) not possible.
There is an open vote for adding that functionality for multiple selections so you might want to vote there.
One idea of mine would be to try to select a folder path instead and load all textures from that folder but that is only a workaround and not really what you requested.
But I hope the rest helps you a bit :)
So the question is this: I have states in my code. I want to divide them to regions and simply enable or disable them upon some conditions. The code is in a looping Unity function named LateUpdate. I wish to do this to save up on processor time instead of using booleans or enums.
Can I do that? if so, how?
Add. info: I need to do this disabling on runtime. The script is an image sequence player that plays gifs. I have a warm up part then a looping part. I can write it with bools but as I said I need to save on processor time as this will be a mobile game.
some code:
void LateUpdate ()
{
if (warmUp)
{
currentImageIndex = Mathf.RoundToInt(Time.time * frameRate);
currentImageIndex = currentImageIndex % spriteSheet.Length;
spriteRenderer.sprite = spriteSheet[currentImageIndex];
warmUp = false;
}
if (updateEnabled && !warmUp)
{
switch (playMode)
{
case PlayMode.order:
currentImageIndex = Mathf.RoundToInt(Time.time * frameRate);
currentImageIndex = currentImageIndex % spriteSheet.Length;
spriteRenderer.sprite = spriteSheet[currentImageIndex];
break;
case PlayMode.random:
updateEnabled = false;
StartCoroutine(RandomizedPlay());
break;
}
}
}
Simple if checks like you are currently doing already is the correct way to disable code at runtime that does not always need to run inside a function.
You should learn how to use the unity profiler on the mobile device, the source of your slowdown (if you are having any) are not from your if statements themselves.
I would say it is extremely likely that each of those if checks won't even get above 0.00% of the CPU time during the profiler.
I have implemented basic Hardware model instancing method in XNA code by following this short tutorial:
http://www.float4x4.net/index.php/2011/07/hardware-instancing-for-pc-in-xna-4-with-textures/
I have created the needed shader (without texture atlas though, single texture only) and I am trying to use this method to draw a simple tree I generated using 3DS Max 2013 and exported via FBX format.
The results I'm seeing left me without clue as to what is going on.
Back when I was using no instancing methods, but simply calling Draw on a mesh (for every tree on a level), the whole tree was shown:
I have made absolutely sure that the Model contains only one Mesh and that Mesh contains only one MeshPart.
I am using Vertex Extraction method, by using Model's Vertex and Index Buffer "GetData<>()" method, and correct number of vertices and indices, hence, correct number of primitives is rendered. Correct texture coordinates and Normals for lighting are also extracted, as is visible by the part of the tree that is being rendered.
Also the parts of the tree are also in their correct places as well.
They are simply missing some 1000 or so polygons for absolutely no reason what so ever. I have break-pointed at every step of vertex extraction and shader's parameter generation, and I cannot for the life of me figure out what am I doing wrong.
My Shader's Vertex Transformation function:
VertexShaderOutput VertexShaderFunction2(VertexShaderInput IN, float4x4 instanceTransform : TEXCOORD1)
{
VertexShaderOutput output;
float4 worldPosition = mul(IN.Position, transpose(instanceTransform));
float4 viewPosition = mul(worldPosition, View);
output.Position = mul(viewPosition, Projection);
output.texCoord = IN.texCoord;
output.Normal = IN.Normal;
return output;
}
Vertex bindings and index buffer generation:
instanceBuffer = new VertexBuffer(Game1.graphics.GraphicsDevice, Core.VertexData.InstanceVertex.vertexDeclaration, counter, BufferUsage.WriteOnly);
instanceVertices = new Core.VertexData.InstanceVertex[counter];
for (int i = 0; i < counter; i++)
{
instanceVertices[i] = new Core.VertexData.InstanceVertex(locations[i]);
}
instanceBuffer.SetData(instanceVertices);
bufferBinding[0] = new VertexBufferBinding(vBuffer, 0, 0);
bufferBinding[1] = new VertexBufferBinding(instanceBuffer, 0, 1);
Vertex extraction method used to get all vertex info (this part I'm sure works correctly as I have used it before to load test geometric shapes into levels, like boxes, spheres, etc for testing various shaders, and constructing bounding boxes around them using extracted vertex data, and it is all correct):
public void getVertexData(ModelMeshPart part)
{
modelVertices = new VertexPositionNormalTexture[part.NumVertices];
rawData = new Vector3[modelVertices.Length];
modelIndices32 = new uint[rawData.Length];
modelIndices16 = new ushort[rawData.Length];
int stride = part.VertexBuffer.VertexDeclaration.VertexStride;
VertexPositionNormalTexture[] vertexData = new VertexPositionNormalTexture[part.NumVertices];
part.VertexBuffer.GetData(part.VertexOffset * stride, vertexData, 0, part.NumVertices, stride);
if (part.IndexBuffer.IndexElementSize == IndexElementSize.ThirtyTwoBits)
part.IndexBuffer.GetData<uint>(modelIndices32);
if (part.IndexBuffer.IndexElementSize == IndexElementSize.SixteenBits)
part.IndexBuffer.GetData<ushort>(modelIndices16);
for (int i = 0; i < modelVertices.Length; i++)
{
rawData[i] = vertexData[i].Position;
modelVertices[i].Position = rawData[i];
modelVertices[i].TextureCoordinate = vertexData[i].TextureCoordinate;
modelVertices[i].Normal = vertexData[i].Normal;
counter++;
}
}
This is the rendering code for the object batch (trees in this particular case):
public void RenderHW()
{
Game1.graphics.GraphicsDevice.RasterizerState = rState;
treeBatchShader.CurrentTechnique.Passes[0].Apply();
Game1.graphics.GraphicsDevice.SetVertexBuffers(bufferBinding);
Game1.graphics.GraphicsDevice.Indices = iBuffer;
Game1.graphics.GraphicsDevice.DrawInstancedPrimitives(PrimitiveType.TriangleList, 0, 0, treeMesh.Length, 0, primitive , counter);
Game1.graphics.GraphicsDevice.RasterizerState = rState2;
}
If anybody has any idea where to even start looking for errors, just post all ideas that come to mind, as I'm completely stumped as to what's going on.
This even counters all my previous experience where I'd mess something up in shader code or vertex generation, you'd get some absolute mess on your screen - numerous graphical artifacts such as elongated triangles originating where mesh should be, but one tip stretching back to (0,0,0), black texture, incorrect positioning (often outside skybox or below terrain), incorrect scaling...
This is something different, almost as if it works - the part of the tree that is visible is correct in every single aspect (location, rotation, scale, texture, shading), except that a part is missing. What makes it weirder for me is that the part missing is seemingly logically segmented: Only tree trunk's primitives, and some leaves off the lowest branches of the tree are missing, leaving all other primitives correctly rendered with no artifacts. Basically, they're... correctly missing.
Solved. Of course it was the one part I was 100% sure it was correct while it was not.
modelIndices32 = new uint[rawData.Length];
modelIndices16 = new ushort[rawData.Length];
Change that into:
modelIndices32 = new uint[part.IndexBuffer.IndexCount];
modelIndices16 = new ushort[part.IndexBuffer.IndexCount];
Now I have to just figure out why are 3 draw calls rendering 300 trees slower than 300 draw calls rendering 1 tree each (i.e. why did I waste entire afternoon creating a new problem).
I've modified the SuperContextMenuStrip found at CodeProject to meet some of my projects needs. I'm using it as a tooltip for map markers on a GMap.NET Map Control. Here is a sample of what it looks like:
What I would like to do is pretty this up a little by making it look more like a bubble. Similar to an old Google Maps stytle tooltip:
I've spent some time searching on control transparency and I know this isn't an easy thing. This SO question in particular illustrates that.
I have considered overriding the OnPaint method of the SuperContextMenuStrip to draw a background of the GMap.NET control that is underneath the SuperContextMenuStrip, but even that would fail in cases where the marker is hanging off the GMap.NET control:
What is the correct way to create the type of transparency I am looking for?
In Windows Forms, you achieve transparency (or draw irregularly shaped windows) by defining a region. To quote MSDN
The window region is a collection of pixels within the window where
the operating system permits drawing.
In your case, you should have a bitmap that you will use as a mask. The bitmap should have at least two distinct colors. One of these colors should represent the part of the control that you want to be transparent.
You would then create a region like this:
// this code assumes that the pixel 0, 0 (the pixel at the top, left corner)
// of the bitmap passed contains the color you wish to make transparent.
private static Region CreateRegion(Bitmap maskImage) {
Color mask = maskImage.GetPixel(0, 0);
GraphicsPath grapicsPath = new GraphicsPath();
for (int x = 0; x < maskImage.Width; x++) {
for (int y = 0; y < maskImage.Height; y++) {
if (!maskImage.GetPixel(x, y).Equals(mask)) {
grapicsPath.AddRectangle(new Rectangle(x, y, 1, 1));
}
}
}
return new Region(grapicsPath);
}
You would then set the control’s Region to the Region returned by the CreateRegion method.
this.Region = CreateRegion(YourMaskBitmap);
to remove the transparency:
this.Region = new Region();
As you can probably tell from the code above, creating regions is expensive resource-wise. I'd advice saving regions in variables should you need to use them multiple times. If you use cached regions this way, you'd soon experience another problem. The assignment would work the first time but you would get an ObjectDisposedException on subsequent calls.
A little investigation with refrector would reveal the following code within the set accessor of the Region Property:
this.Properties.SetObject(PropRegion, value);
if (region != null)
{
region.Dispose();
}
The Region object is disposed after use!
Luckily, the Region is clonable and all you need to do to preserve your Region object is to assign a clone:
private Region _myRegion = null;
private void SomeMethod() {
_myRegion = CreateRegion(YourMaskBitmap);
}
private void SomeOtherMethod() {
this.Region = _myRegion.Clone();
}
I'm currently working my way through "Beginning C# Programming", and have hit a problem in chapter 7 when drawing textures.
I have used the same code as on the demo CD, and although I had to change the path of the texture to be absolute, when rendered it is appearing grey.
I have debugged the program to write to file the loaded texture, and this is fine - no problems there. So something after that point is going wrong.
Here are some snippets of code:
public void InitializeGraphics()
{
// set up the parameters
Direct3D.PresentParameters p = new Direct3D.PresentParameters();
p.SwapEffect = Direct3D.SwapEffect.Discard;
...
graphics = new Direct3D.Device( 0, Direct3D.DeviceType.Hardware, this,
Direct3D.CreateFlags.SoftwareVertexProcessing, p );
...
// set up various drawing options
graphics.RenderState.CullMode = Direct3D.Cull.None;
graphics.RenderState.AlphaBlendEnable = true;
graphics.RenderState.AlphaBlendOperation = Direct3D.BlendOperation.Add;
graphics.RenderState.DestinationBlend = Direct3D.Blend.InvSourceAlpha;
graphics.RenderState.SourceBlend = Direct3D.Blend.SourceAlpha;
...
}
public void InitializeGeometry()
{
...
texture = Direct3D.TextureLoader.FromFile(
graphics, "E:\\Programming\\SharpDevelop_Projects\\AdvancedFrameworkv2\\texture.jpg", 0, 0, 0, 0, Direct3D.Format.Unknown,
Direct3D.Pool.Managed, Direct3D.Filter.Linear,
Direct3D.Filter.Linear, 0 );
...
}
protected virtual void Render()
{
graphics.Clear( Direct3D.ClearFlags.Target, Color.White , 1.0f, 0 );
graphics.BeginScene();
// set the texture
graphics.SetTexture( 0, texture );
// set the vertex format
graphics.VertexFormat = Direct3D.CustomVertex.TransformedTextured.Format;
// draw the triangles
graphics.DrawUserPrimitives( Direct3D.PrimitiveType.TriangleStrip, 2, vertexes );
graphics.EndScene();
graphics.Present();
...
}
I can't figure out what is going wrong here. Obviously if I load up the texture in windows it displays fine - so there's something not right in the code examples given in the book. It doesn't actually work, and there must be something wrong with my environment presumably.
You're using a REALLY old technology there... I'm guessing you're trying to make a game (as we all did when we started out!), try using XNA. My best guess is that it's your graphics driver. I know that sounds like a cop out, but seriously, I've seen this before and once I swapped out my old graphics card for a new one it worked! I'm not saying it's broken, or that it's impossible to get it to work. But my best two suggestions would be to:
1) Start using XNA and use the tutorials on http://www.xnadevelopment.com/tutorials.shtml
2) Replace your graphics card (if you want to carry on with what you are doing now).